Python handles globals badly.

Ian Kelly ian.g.kelly at gmail.com
Wed Sep 2 18:57:16 EDT 2015


On Wed, Sep 2, 2015 at 4:25 PM,  <tdev at freenet.de> wrote:
> That said, it is not an overusing of globals cause globals are module vars only.
> Or have you never written singletons or other classes in e.g Java, C++ with
> lots of static and class members and methods using this members.

I do use lots of static members in Java. 99.99% of them are constants,
which if translated to Python globals would not require a global
declaration anyway.

Class members are not like Python globals. Class members are like
Python class attributes.

> With globals in Python are not meant traditional globals (app spanning vars) as already said.
> Globals in Python are like class-members and statics in OO-languages. Globals are module scope!
> And therefore it is not the devil or evil problem or code desaster problem
> or LUA's opposite approach.
>
> (local/nonlocal statements I think are needed cause of lambda functionality and
>  is not what I try to discuss here)

The local statement I was talking about has nothing to do with
closures, which I think is what you mean. Within a given function, the
compiler needs to have some way of knowing whether a given name is
local or global. Currently that's done by the "global" keyword and by
checking whether the name is ever assigned within the function.

If the "global" keyword is no longer required, then there would have
to be some other way for the compiler to distinguish locals from
globals. The obvious solution would be to use a "local" keyword
instead. That seems to me like it would be a lot more annoying.



More information about the Python-list mailing list