[Python-ideas] Make all keywords legal as an attribute name

Arnaud Delobelle arnodel at gmail.com
Tue Apr 26 09:05:28 CEST 2011


On 26 Apr 2011, at 07:42, Mike Meyer wrote:

> On Mon, 25 Apr 2011 16:26:47 -0400
> Mike Graham <mikegraham at gmail.com> wrote:
>> Other examples, some more contrived than others, could be
>> provided--some of these would be good names if not for their keyword
>> status. However, I don't think I've seen a suggestion better than the
>> current solution (or lack thereof).
> 
> I think the *first* part of this proposal - allowing attribute names
> to be keywords - provides almost all the benefits and few of the
> problems that were brought up.

Note that attribute names can already be keywords (same for globals).  It's just that the compiler will complain when it sees them, so you have to make sure it doesn't sees them.

>>> class A: pass
... 
>>> a = A()
>>> setattr(a, "with", 1)
>>> getattr(a, "with")
1

>>> globals()["for"] = 12
>>> globals()["for"]
12

-- 
Arnaud




More information about the Python-ideas mailing list