None assigment

D-Man dsh8290 at rit.edu
Thu Feb 8 19:15:23 CET 2001


On Thu, Feb 08, 2001 at 05:34:37PM +0000, Simon Brunning wrote:
| > 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.

I didn't realize this, and had never tried. 

|  
| > 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.

I didn't realize this either, and had never tried.  Check this out :

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> del None
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  NameError: There is no variable named 'None'
>>>
>>> None = 1
>>> del None
>>> del None
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  NameError: There is no variable named 'None'
>>>


Interesting.  So normally "None" is a special keyword (that is a
reference to an object) ,  but that doesn't prevent one from creating
a local variable named "None" that shadows the keyword.

:-)
-D





More information about the Python-list mailing list