[Python-Dev] Assignment to None

Michael Foord fuzzyman at voidspace.org.uk
Mon Jun 9 10:28:03 CEST 2008


Steven D'Aprano wrote:
> 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.
>
>   

FWIW Curt is asking because he is on the team responsible for 
implementing IronPython.

In .NET you have several enumerations with members called None, for 
example 'Alignment.None'. It would be inconvenient for IronPython users 
if it enforced this particular syntax rule of Python's; that not only is 
assignment to None disallowed but that members named None are invalid 
syntax.

The question is, what is the specification for Python. Is it that 
assignment to None is disallowed and the naming of members called None 
being invalid syntax is merely an artefact of the implementation of 
this, or does Python require this...

Michael Foord

> 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')
>>>>
>>>>         
>
>
>   



More information about the Python-Dev mailing list