[Python-Dev] Assignment to None

Benjamin Peterson musiccomposition at gmail.com
Mon Jun 9 05:10:12 CEST 2008


On Sun, Jun 8, 2008 at 9:24 PM, Curt Hagenlocher <curt at hagenlocher.org> wrote:
> My apologies if this is one of those "dead horse" issues.  The
> following seems a little inconsistent to me:
>
>>>> c = C()
>>>> c.None
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AttributeError: C instance has no attribute 'None'
>>>> c.None = 'foo'
>  File "<stdin>", line 1
> SyntaxError: assignment to None
>>>> setattr(c, 'None', 'foo')
>>>> c.None
> 'foo'
>>>>
>
> So, it's okay to setattr the attribute name "None" but not okay to set
> it directly?  Is this deliberate or is it an unintentional side effect
> of parser changes to prevent assignment to None?

I believe that runtime checks like the one needed for setattr would be
too much of a performance hit to be practical. The syntax changes are
meant to avoid mistakes and confusion rather than completely prevent
something from ever happening.



-- 
Cheers,
Benjamin Peterson
"There's no place like 127.0.0.1."


More information about the Python-Dev mailing list