[Python-3000] PEP for Metaclasses in Python 3000

Talin talin at acm.org
Sat Mar 10 10:13:44 CET 2007


Josiah Carlson wrote:
> Also, depending on the use, one may want to know the order in a 'move to
> end' fashion (if a is assigned to multiple times, it ends up in the
> ordering as if only the last assignment was done).

This is why I feel it is insufficient to keep just a record of what key 
names were inserted, it should be a record of both keys and values. So 
for example, if we have:

    class A:
       b = 1
       b = 2

...the resulting list should be [(b, 1), (b, 2)]

This gives the metaclass a lot of flexibility for handling duplicates.
Suppose, for example, a metaclass wants to handle duplicates by keeping 
only the last definition. This is easy to do:

    newdict = {}
    for key, value in orderlist:
       newdict[ key ] = value  # Overwrite any previous definition

What if you only want the *first* definition? Then check to see if the 
key is already in newdict and skip the assignment.

What if you want to flag duplicates as an error? Check to see if the key 
is already in newdict, and throw an exception if so.

You can even combine or concatenate the duplicates in some way if you 
keep enough information around to do so. It's up to the metaclass to decide.

-- Talin


More information about the Python-3000 mailing list