How should we use global variables correctly?
On 23Aug2019 09:07, Frank Millman <frank at chagford.com> wrote:
>On 2019-08-23 8:43 AM, Windson Yang wrote:
>> class Example:
>> def __init__(self):
>> self.foo = 1
>> def bar()
>> return self.foo + 1
>>Expect the syntax, why using class variable self.foo would be better (or
>>more common)? I think the 'global' here is relative, foo is global in
>>global.py and self.foo is global in Example class. If the global.py is
>>short and clean enough (didn't have a lot of other class), they are pretty
>>much the same. Or I missed something?
>One difference is that you could have many instances of Example, each
>with its own value of 'foo', whereas with a global 'foo' there can
>only be one value of 'foo' for the module.
But that is an _instance_ attribute. Which is actually what Windson Yang
A class attribute is bound to the class, not an instance. The
terminology is important.
Cameron Simpson <cs at cskk.id.au>