Python handles globals badly.

Steven D'Aprano steve at pearwood.info
Wed Sep 2 21:49:39 EDT 2015


On Thu, 3 Sep 2015 04:47 am, tdev at freenet.de wrote:

> I agree with Skybuck Flying.
> I am aware if a var is a module function var or a module global var.

Congratulations!

But you're not the Python compiler, which doesn't have your superior insight
and intuition.

Inside a function, how is the compiler supposed to know whether "x = 1"
refers to a global or local?

If you can explain that, then your proposal has the tiniest chance of
success. Without that explanation, it has no hope at all.


[...]
> Especially cause a module is a singleton.

What does that have to do with anything?


> And globals are only module aware vars.

What does that even mean?


> Even Java (type-safe language) need not such things for its static or
> member vars. 

Java has different rules for variables -- all variables are statically
defined in Java and known to the compiler, that is not the case with
dynamic languages like Lua, Python, Javascript, PHP.

You can't do this in Java:

name = random.choice(["foo", "bar", "baz"])
exec ("%s = 42" % name+"abc", globals())
globals()[name.upper()] = 999

So tell me, what global variables do you have now?


> I have disliked this already in PHP where one has to do 
> exact the same thing as in Python (and hoped Python does it better, but
> not). And using an import here is no solution, cause this is
> something which relates only to the scope and state of a module itself.

Again, I don't understand what your comment even means.


-- 
Steven




More information about the Python-list mailing list