[Python-Dev] Assignment to None

Steven D'Aprano steve at pearwood.info
Mon Jun 9 05:48:44 CEST 2008


On Mon, 9 Jun 2008 12:24:55 pm Curt Hagenlocher wrote:

> So, it's okay to setattr the attribute name "None" but not okay to 
> set it directly?  

I suspect this is off-topic for python-dev, and would be better on 
comp.lang.python or similar, but for what it's worth, I consider having 
an attribute named 'None' bad practise, regardless of any runtime 
checks. But as part of Python's "we're all consenting adults here" 
philosophy, I wouldn't approve of expensive or extensive run-time 
checks specifically to prevent it. If you really have to name your 
attribute None, and are prepared to live with the consequences, then go 
ahead.

In a similar fashion:

>>> class Parrot(object):
...     pass
...
>>> p = Parrot()
>>> p.1 = 'spam'
  File "<stdin>", line 1
    p.1
      ^
SyntaxError: invalid syntax
>>> setattr(p, '1', 'spam')
>>> 


-- 
Steven


More information about the Python-Dev mailing list