[Python-Dev] Assignment to None

Terry Reedy tjreedy at udel.edu
Mon Jun 9 21:22:39 CEST 2008


"Tony Nelson" <tonynelson at georgeanelson.com> wrote in message 
news:p04330107c4730af274e7@[192.168.123.162]...
| At 4:46 PM +0100 6/9/08, Michael Foord wrote:
| Or perhaps CPython should just stop trying to detect this at compile 
time.
| Note that while assignment to ".None" is not allowed, setattr(foo, 
"None",
| 1) then referencing ".None" is allowed.
|
| >>> f.None = 1
| SyntaxError: assignment to None
| >>> f.None
| Traceback (most recent call last):
|  File "<stdin>", line 1, in ?
| AttributeError: 'Foo' object has no attribute 'None'
| >>> setattr(f, 'None', 1)
| > f.None
| 1

I was a little surprised by this.  ISTM that f.None should consistently 
work or not work both for getting and setting.  The last result could be 
considered a bug rusulting from getting disabling depending on None not 
being set, but having a backdoor that does allow it to be set.

I recently read someone (Guido?) saying that a language should start 
'strict' because it is much easier to allow something new than disallow 
something old.  Allowing 'None' and possibly other keywords as attributes 
will not break code (that does not depend on the exceptions).   But I 
presume there was some thought that the restriction might prevent buggy 
code.

At the global level, the subversion does not work:
>>> globals()['None'] = 'ha'
>>> None
>>> dir()
['None', '__builtins__', '__doc__', '__name__', '__package__']
>>> globals()['None']
'ha'

tjr





More information about the Python-Dev mailing list