[Python-Dev] Things to Know About Super (original) (raw)
Alex Martelli aleaxit at gmail.com
Wed Aug 27 16:30:29 CEST 2008
- Previous message: [Python-Dev] Things to Know About Super
- Next message: [Python-Dev] Things to Know About Super
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, Aug 26, 2008 at 10:24 PM, Michele Simionato <michele.simionato at gmail.com> wrote: ...
.. code-block:: python
def includemixin(mixin, cls): # could be extended to use more mixins # traits as in Squeak take the precedence over the base class dic = vars(mixin).copy() # could be extended to walk the ancestors return type(mixin.name + cls.name, (cls,), dic)
I don't see any use of this in your following example so I assume you're introducing it just to be able to say that:
In the fictional world there is not need for super since all hierarchies are linear and you can just call the base class;
Nevertheless I must be missing something in the following example:
FrameworkMeta could have been written as
.. code-block:: python class FrameworkMeta2(type): def new(mcl, name, bases, dic): print "Adding framework features to %s" % name return type.new(mcl, name, bases, dic)
and DebugMetas as .. code-block:: python class DebugMeta2(type): def new(mcl, name, bases, dic): print "Adding debugging features to %s" % name return mcl.base.new(mcl, name, bases, dic) Notice that DebugMeta2 is performing a sort of cooperative call here (
mcl._base_._new_
) but dead simple since there is just one base class. The analogous of FrameworkClass can be defined asclass FrameworkClass2(object): ... metaclass = FrameworkMeta2 Adding framework features to FrameworkClass2 and the analogous of DebugFrameworkClass as class DebugFrameworkClass2(FrameworkClass2): ... metaclass = DebugFrameworkMeta2
What's DebugFrameworkMeta2? I assume it's some kind of mix but I don't see it defined anywhere so I'm having to guess.
Adding debugging features to DebugFrameworkClass2 Adding framework features to DebugFrameworkClass2
But where does DebugMeta2 get injected or otherwise get into the picture in this example, so that the "Adding debugging features" print executes? I don't see any code in your example that performs this injection.
Maybe you're missing a key bit where you build DebugFrameworkMeta2 by using that example include_mixin you have?
I'd like to understand what, in this example, removes the apparent "fragility" (or, lack of flexibility) where DebugMeta2 specifically uses mcl.base -- IOW, if I have another "mixin metaclass" just like DebugMeta2 but called (say) RemotableMeta which does a print "Adding remoting features" and then also calls mcl.base.new(mcl ... just like DebugMeta2, what gets both of their new methods called in the right order?
Maybe you could help me understand this by giving a fully executable Python snippet using bases[0] instead of the hypothetical base?
A;ex
- Previous message: [Python-Dev] Things to Know About Super
- Next message: [Python-Dev] Things to Know About Super
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]