None assigment

Rainer Deyke root at rainerdeyke.com
Thu Feb 8 14:45:30 EST 2001


"Simon Brunning" <SBrunning at trisystems.co.uk> wrote in message
news:mailman.981653763.25307.python-list at python.org...
> > From: Gregoire Welraeds [SMTP:greg at perceval.be]
> > > After doing "None = 2", you can "del None" to get the default value
> > > back.
>
> Hmm. This is true - I didn't know that.
>
> > If we follow that logic, I could use any non assigned variable to have
the
> > following working:
> >
> > >>> a= [1,'',3]
> > >>> filter(b,a)
> >
> > but this won't work as the interpreter complains that there is no
variable
> > named b.
>
> You can't do this with *ordinary* labels - by del'ing them, you are
> un-assigning them. But the 'None' label is *extraordinary*, obviously.

Actually, it's just in '__builtins__'.  When you assign to 'None', you
create a variable in the module namespace which hides '__builtins__.None'.
When you delete it, the 'None' in '__builtins__' is revealed.  This applies
to all labels:

__builtins__.foo = 1
print foo # Prints 1.
foo = 5
print foo # Prints 5.
del foo
print foo # Prints 1 again.  Neat, huh?


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list