the global keyword:

Rick Johnson rantingrickjohnson at gmail.com
Tue Jun 21 18:20:34 EDT 2016


On Tuesday, June 21, 2016 at 12:15:38 PM UTC-5, Random832 wrote:
> You can put a function or constant there, sure. But if
> you're using it as a variable, you'd have to do that
> *every* time (in which case what's the point) 

Well, since the term "variable" has been so abused, using it
in such a narrowly defined manner here creates a high
probability of starting a semantics flame-war. (c) python-list

Depending on the specific inputs, the "data a variable
symbol is attached to" may indeed remain static throughout
the entire life of the program -- Hmmm -- in that case,
should we still call it a variable? I suppose it's a matter
of deciding if reality defines a variable, or if programmer
intent defines a variable.

Metaphysical musings aside, i get your point. O:-).

> because reassigning to a builtin by name won't reassign
> the builtin, it'll create a new variable in the current
> context.

True. As can be demonstrated with this interactive session.

    py> import sys
    py> def foo():
    ...     print 'My name is Foo!'
    py> sys.modules['__builtin__'].__dict__['foo_func'] = foo
    py> foo()
    My name is Foo!
    py> foo = 'bar'
    py> foo
    'bar'
    py> sys.modules['__builtin__'].__dict__['foo_func']
    <function foo at 0x02810330>

Storing dynamic data to global space is almost always
foolish, and I'm a fan of name spaces. But i did not
recommend such foolish action, i was merely replying to the
assertion that "Python does have real globals". She does,
you just have look under the tail! 

  (sexual identity yet to be determined)

Besides, Python's global statement is misleading to noobs.
It's unfortunate that Python did not inject a symbol to
represent the module level space. Something like "M" or
"mod", or my personal favorite "MSFL"! (ModuleSpaceForLife)
Then one could have added module-level symbols without all
the semantic hubbub.

    MSFL.foo = 0
    
    def iter_foo():
        MSFL.foo += 1

Heck, when i see the global statement in Python code, i
immediately pinch my nose, and run away. There may be
legitimate uses for the global statement, but i've yet to
see them.



More information about the Python-list mailing list