None assigment

Christian Tanzer tanzer at swing.co.at
Fri Feb 9 07:19:04 CET 2001


D-Man <dsh8290 at rit.edu> wrote:

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

None is not a keyword. It's a binding living in the __builtin__ scope.
Therefore a plain `del None' doesn't work.

Try this:

tanzer [lib] 2 $ python2.0 
Python 2.0 (#5, Jan 30 2001, 11:08:38) 
[GCC 2.7.2.1] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import __builtin__
>>> del __builtin__.None 
>>> None
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'None'
>>> 

As others repeatedly pointed out, it's not a good idea to do such
things in real code.

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list