
On 28/07/2011 04:57, Nick Coghlan wrote:
On Thu, Jul 28, 2011 at 12:21 PM, Guido van Rossum<guido@python.org> wrote:
I wish I could write
red = Enum(1)
and it made the following true:
assert red == 1 assert isinstance(red, int) # a subclass assert str(red) == 'red'
But we'd first need a non-hacky way for Enum() to know that it is being assigned to something named 'red'. I have a few other use cases for that as well, e.g. a Property class for App Engine that doesn't require a metaclass to patch up the value.
Yeah, a similar discussion came up in the context of defining namedtuple instances a while back (I don't recall if you were part of that conversation or not - IIRC, the thread started off on the topic of assignment decorators and ended up wandering down this road at some point)
Most proposed solutions relied on some form of abuse of the def statement to define arbitrary objects that knew their own name. For example:
def red from namedvalue(1) # Rather unnatural phrasing def red as namedvalue(1) # Phrasing is natural, but the name is on the wrong side of the 'as' def red = namedvalue(1) # Simple assignment may not suggest enough magic def as red = namedvalue(1) # Syntax soup! as red def namedvalue(1) # Just throw keywords at the screen and see if anything sticks red def= namedvalue(1) # An alternative inspired by augmented assignment def red<< namedvalue(1) # Arbitrary but suggestive
[snip] You missed out: def red is namedvalue(1)