Python handles globals badly.

Michael Torrie torriem at gmail.com
Sat Sep 12 02:34:07 CEST 2015


On 09/11/2015 06:11 PM, random832 at fastmail.us wrote:
> On Fri, Sep 11, 2015, at 20:01, Michael Torrie wrote:
>> The secret to understanding the global keyword is to understand how
>> Python namespaces work.  The statement "a=5" does not assign a 5 to the
>> box called "a."  Rather it binds the name "a" to the "5" object, which
>> is immutable and called into existence by the interpreter
>> implementation.
> 
> In other words, it assigns a pointer to the "5" object [otherwise known
> as "a 5"] to the box called "a". (And increments its reference count, if
> you care about how the CPython garbage collector works)

Yes I suppose that works. It shows the difference between Pascal's "a :=
5" and Python's "a = 5".  So long as it's not just a pointer we're
talking about here; it's a counted reference. I think I prefer the word
"reference" to "pointer" in this case.  The word, pointer has certain
connotations that come from C.  Certainly in the implementation you
would probably use pointers.  But we don't really need a full physical
memory abstraction to understand names and how they are bound (made to
refer) to objects.  In fact it might be more useful to forget about the
underlying mechanisms in the interpreter.  And it's useful to think
about namespaces because then we can understand what happens when python
sees a variable in an expression. In any case, we're not overwriting any
values in Python assignments.



More information about the Python-list mailing list