[Python-3000] rethinking pep 3115

Alex Martelli aleaxit at gmail.com
Mon Jun 11 18:51:06 CEST 2007


On Jun 11, 2007, at 4:10 PM, Nick Coghlan wrote:

Alex Martelli wrote:
(2)
the second-best solution i could think of is just passing the dict as a
keyword argument to the class, like so:

class Spam(metaclass = Bacon, dict = {}):
    ...

so you could explicitly state you need a special dict.
I like this one, with classdict being the keyword (dict is the name of
a builtin type and we shouldn't encourage the frequent but iffy
practice of 'overriding' builtin identifiers).

So instead of being able to write:

  class MyStruct(Struct):
     first = 1
     second = 2
     third = 3

everyone defining a Struct subclass has to write:

  class MyStruct(Struct, classdict=OrderedDict()):
     first = 1
     second = 2
     third = 3

Forgive my confusion, but exactly *how* is that meant to be an improvement?

Why can't the classdict get inherited just like the metaclass can?

I'm not sure, btw, if we want the classdict to be an _instance_ of a
mapping, exactly because of inheritance -- a type or factory seems
more natural to me.  Sure, the metaclass might deal with that if
necessary, but I don't see an advantage in making it have to do so.
Thus, I'd use classdict=dict instead of classdict={}, etc.

The use of a special ordered dictionary should be an internal
implementation detail of the Struct class, and PEP 3115 makes it
exactly that. The PEP's approach means that simple cases, while
possibly being slightly harder to write, will 'just work' when it
comes time to use them, while more complicated cases involving
multiple metaclasses should still be possible.

I will also note that the PEP allows someone to write their own base
class which accepts the 'classdict' keyword argument if they so
choose.

The PEP seems to allow a whole lot of things.  I'm with Tomer in
wondering whether this lot may be too much.


Alex


More information about the Python-3000 mailing list