Overriding a global

Joshua Landau joshua.landau.ws at gmail.com
Mon Dec 12 18:48:09 EST 2011


>
> > If a function knows of the presence of a global, it's not asking too
> > much for it to not re-use the same name in local scope.
>
> Yes.


It's just a function wanting to act as-if it were in a different
environment than its default. By that same reasoning you could state that
"If a function knows of the presence of a built-in, it's not asking
too much for it to not re-use the same name in local scope." Yet if
rebinding "id" is such a crime, why is it so oft done? Rebinding logger
locally in a function is really no different to a subclass rebinding a
variable from its main class using that class' value. *The only difference
is that, in that case, you have an alternate binding to the original value.*
*
*
>>> class A():
...     val = 1
...
>>> class B(A):
...     val = str(val) # Obviously, this doesn't work
...
<SNIP>
NameError: name 'val' is not defined
>>> class B(A):
...     val = str(A.val) # But it's OK as we can just do this ^^
...
>>> B().val
'1'
>>>

The only reason it's not minded with classes is because there's a good way
to do it.

I get that my analogy doesn't use globals, but the idea of extending a
more-global attribute locally is shared between the concepts.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111212/3d50039b/attachment-0001.html>


More information about the Python-list mailing list