[Python-Dev] Things to Know About Super

Alex Martelli aleaxit at gmail.com
Wed Aug 27 16:30:29 CEST 2008


On Tue, Aug 26, 2008 at 10:24 PM, Michele Simionato
<michele.simionato at gmail.com> wrote:
   ...
> .. code-block:: python
>
>  def include_mixin(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 as
>
>>>> class 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


More information about the Python-Dev mailing list