[Python-ideas] anonymous object support

Eric Snow ericsnowcurrently at gmail.com
Sat Aug 6 08:53:43 CEST 2011

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

You don't even need to use __prepare__() if you make it look like this:

class Point:
    x = ...
    y = ...

Since Ellipsis became valid as a normal-use object this works.  And
that decorator is a snap to write.


> I sort of like this syntax, but I think it would crazy to use in production, because who besides the original creator of the hack would guess that class NamedTuple has a metaclass which uses the __prepare__ method to record the fields accessed during class creation? But with a namespace keyword you signal to the reader of the code: "Here be meta-dragons! Proceed with caution." The counterargument can be made, do we really want to encourage people to fiddle around with metaclasses any more than they already do? But my response to that is that people are already making ORMs and whatnot using the class keyword, so why not pave the cowpath? Also we could throw in some built in tools to make the metaclasses less confusing to work with.
> Also, hey free switch statement ;-) :
> key = random.choice(["case1", "case2"])
> namespace dispatch(dict):
>        def case1(): print("One")
>        def case2(): print("Two")
> dispatch[key]()
> -- Carl
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list