Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • Can I add this on variable on globals variables? How do you track global variable's value change? I am not talking about a function or a class object or attributes? Let's say we have a global variable x=10;. What I want is when an new value is assigned to x then I want the observer function to invoke. setattribute on the object class to do this doesnt work. Commented Jan 12, 2020 at 10:55
  • stackoverflow.com/questions/59675250/… Commented Jan 12, 2020 at 10:59
  • @Gary No. Everything is an object in Python (numbers, functions, strings, everything, except maybe operators, but you can modify what they do, too). You can use global variables that are objects, however. If you need more transparency (so you don't feel like it's an object), define more of those methods that begin and end with double underscores. You track a global variable value change the same as any other variable. It's just a scope. Commented Jan 16, 2020 at 14:03
  • You can't overload assignment for a plain variable. However, you can overload assignment for attributes/properties of an object. See this question: stackoverflow.com/questions/11024646/…. So, you could do x.x=5 and have it call a function, but not just x=5. The variables themselves aren't actually objects (AFAIK). They're just names. Commented Jan 16, 2020 at 14:48
  • You can overload += and other such, though, with __iadd__ (and other such). Commented Jan 16, 2020 at 14:56