Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python performs no implicit calls to the <code>__init__</code> methods of a class's super-class(es)—but it's possible to make it happen automatically. One way is by defining a metaclass for your mixed class(es) that creates or extends the mixed class's <code>__init__</code> method so that it calls to all the listed bases' <code>__init__</code> functions in the order they were listed.</p> <p>A second way is to do it is to use a class decorator—which is shown below in the section subtitled <strong>Edit</strong>.</p> <p>Using a metaclass:</p> <pre><code>class APIBaseClassOne(object): # API class (can't be changed) def __init__(self, *args, **kwargs): print ' APIBaseClassOne.__init__()' class SomeMixin(object): def __init__(self, *args, **kwargs): print ' SomeMixin.__init__()' class MixedClassMeta(type): def __new__(cls, name, bases, classdict): classinit = classdict.get('__init__') # Possibly None. # define an __init__ function for the new class def __init__(self, *args, **kwargs): # call the __init__ functions of all the bases for base in type(self).__bases__: base.__init__(self, *args, **kwargs) # also call any __init__ function that was in the new class if classinit: classinit(self, *args, **kwargs) # add the local function to the new class classdict['__init__'] = __init__ return type.__new__(cls, name, bases, classdict) class MixedClass(APIBaseClassOne, SomeMixin): __metaclass__ = MixedClassMeta # important # if exists, is called after the __init__'s of all the direct bases def __init__(self, *args, **kwargs): print ' MixedClass.__init__()' print('MixedClass():') MixedClass() </code></pre> <p>Output:</p> <pre class="lang-none prettyprint-override"><code>MixedClass(): APIBaseClassOne.__init__() SomeMixin.__init__() MixedClass.__init__() </code></pre> <p><strong>Edit</strong></p> <p>Here's how to accomplish the same thing with a class decorator (requires Python 2.6+):</p> <pre><code>class APIBaseClassOne(object): # API class (can't be changed) def __init__(self, *args, **kwargs): print)' APIBaseClassOne.__init__()') class SomeMixin(object): def __init__(self, *args, **kwargs): print(' SomeMixin.__init__()') def mixedomatic(cls): """ Mixed-in class decorator. """ classinit = cls.__dict__.get('__init__') # Possibly None. # define an __init__ function for the class def __init__(self, *args, **kwargs): # call the __init__ functions of all the bases for base in cls.__bases__: base.__init__(self, *args, **kwargs) # also call any __init__ function that was in the class if classinit: classinit(self, *args, **kwargs) # make the local function the class's __init__ setattr(cls, '__init__', __init__) return cls @mixedomatic class MixedClass(APIBaseClassOne, SomeMixin): # if exists, is called after the __init__'s of all the direct base classes def __init__(self, *args, **kwargs): print(' MixedClass.__init__()') print('MixedClass():') MixedClass() </code></pre> <p>(For Python &lt; 2.6, use <code>MixedClass = mixedomatic(MixedClass)</code> <em>following</em> the class definition.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload