[Python-ideas] anonymous object support

Terry Reedy tjreedy at udel.edu
Sat Aug 6 21:50:52 CEST 2011


On 8/6/2011 2:53 AM, Eric Snow wrote:
> On Sat, Aug 6, 2011 at 12:37 AM, Carl Matthew Johnson
> <cmjohnson.mailinglist at gmail.com>  wrote:
>> As another example, using metaclasses you can change namedtuple's syntax from the somewhat clunky
>>
>> Point = namedtuple('Point', 'x y')
>>
>> to the more elegant but confusing since it uses "class"
>>
>> class Point(NamedTuple):
>>         x
>>         y

If NamedTuple were documented as having a custom metaclass, and if this 
were a common idiom, that would not be too confusing as all.

> You don't even need to use __prepare__() if you make it look like this:
>
> @as_namedtuple
> class Point:
>      x = ...
>      y = ...
>
> Since Ellipsis became valid as a normal-use object this works.  And
> that decorator is a snap to write.

This looks ok to me too. New keywords and syntax should be *very* rare 
and reserved for things that cannot be done so easily with what we have now.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list