Python 2.4 removes None data type?

Tim Peters tim.peters at gmail.com
Fri Mar 4 16:59:12 EST 2005


[gaudetteje at gmail.com]
> I just read in the 'What's New in Python 2.4' document that the None
> data type was converted to a constant:
> http://python.org/doc/2.4/whatsnew/node15.html
>
> """
> # None is now a constant; code that binds a new value to the name
> "None" is now a syntax error.
> """
> 
> So, what's the implications of this?

No implications, for any sane code.  You can no longer do things like this:

>>> None = 2
SyntaxError: assignment to None

That's all.

> I find the lack of explanation a little puzzling, since I've written code that
> compares a variable's type with the 'None' type.  For example, a variable
> would be initialized to 'None' and if it went through a loop unchanged, I could
> determine this at the end by using a conditional type(var) == type(None).

Python's None is a singleton, meaning that there is a single, unique
instance of type type(None).  So in the case you describe, it's
correct, and idiomatic, to test

     if var is None:

instead.  Checking type(var) against type(None) will still work, but
was never the best way to do it.

If you have an expression like

    var = None

that's fine.  You're not changing the binding of name "None" there,
you're changing the binding of name "var".

>  What will type(None) return now?

That hasn't changed:

>>> type(None)
<type 'NoneType'>



More information about the Python-list mailing list