[Python-Dev] Assignment to None

"Martin v. Löwis" martin at v.loewis.de
Mon Jun 9 22:36:05 CEST 2008


> At the global level, the subversion does not work:

I think you are misinterpreting what you are seeing. When you refer to
the global identifier None, the compiler just knows that it must be
the NoneType singleton, and returns it as a constant, without doing any
name lookup. So it isn't that assignment of the global None fails to
be performed; instead, the compiler just ignores any global with the
name None that might have been set.

The compiler doesn't do the same thing for attributes, and rightfully
so: it would surely be surprising if foo.None would yield the NoneType
singleton for any arbitrary foo. Instead, the compiler generates a
regular attribute, which then regularly finds the attribute in the
object's dictionary if it is there.

> I was a little surprised by this.  ISTM that f.None should
> consistently work or not work both for getting and setting.

That will only happen when (if) None becomes a keywords: then
foo.None will be a syntax error. You will still be able to put
attributes named 'None' (or 'import' or 'if') into any object
using getattr/setattr, or by direct access to __dict__.

Regards,
Martin


More information about the Python-Dev mailing list