[Baypiggies] Python3 Metaclass, inheritance and MRO question

Aseem Mohanty ams.fwd at gmail.com
Fri Dec 28 01:03:31 EST 2018


Heh,

I think you might be right. I read his blog post on super and assumed
otherwise. I think I missed the part where he mentioned every method needs
to call super. :sigh:

I guess I need to figure out how to fix that :)

Thanks.
AM

On Thu, Dec 27, 2018 at 9:45 PM Alexander Sanchez <
a.sanchez.stephens at googlemail.com> wrote:

> Reading the docs around super and cooperative multi inheritance I think
> this is around MRO of diamond diagrams. i.e. it is not going to call all
> the way up the MRO.
>
> Take this really simple example:
>
> class One():
>     def __init__(self):
>         print('init_one')
>
> class Two():
>     def __init__(self):
>         print('init_two')
>
> class Three(Two, One):
>     def __init__(self):
>         print('init_three')
>         super().__init__()
>
> three = Three()
>
> init_one and init_two will not be printed without adding super to the
> __init__’s. You might be able to make a metaclass that add the super call
> to the __init__ automatically.
> I tend to think of super as next in line in the class diagram (probably
> because of this talk https://youtu.be/EiOglTERPEo?t=2488 ).
>
> On Thu, 27 Dec 2018 at 21:27, Aseem Mohanty <ams.fwd at gmail.com> wrote:
>
>> Thanks for the response.
>>
>> I realize that calling super in each of those classes will work, what I
>> wanted was the following. Apologies if I was not clear in the original post.
>>
>> A is a declaratively defined class that also derives from B & C
>>
>> To make an instance of A initialize with the class members (specified
>> when declaring A) as instance members and throw away the class members I
>> use the metaclass (SchemaMeta) which is applied via a class decorator
>> (schema) on A.
>>
>> In the metaclass I also setup another base class (SchemaBase) at the top
>> of the MRO so that I can apply some values to its attributes and other
>> things.
>>
>> I have all of that working.
>>
>> The part that does not work is wrapping the __init__ method of A or any
>> others like A (@schema decorated classes) so that [A.]super().__init__(...)
>> automatically gets called. That decoration happens via auto_super_caller.
>>
>> Per python3 super docs, as long as the signature of A, B, C and schema
>> base are co-operative (same sig) *all* the supers will get called. However
>> in this case only the SchemaBase.__init__ gets called and then it stops.
>>
>> I am not quite sure why that is the case. Perhaps in wrapping A's
>> __init__ I am missing some magic that allows the entire MRO to get
>> processed? I can obviously force that by inspecting the funcspec for
>> __init__ for all classes in the MRO but I think I am just doing something
>> wrong here.
>>
>> Thanks.
>> AM
>>
>>
>>
>> On Thu, Dec 27, 2018 at 9:17 PM Alexander Sanchez <
>> a.sanchez.stephens at googlemail.com> wrote:
>>
>>> Not really sure what you are aiming to achieve.
>>> But https://pastebin.com/eY4ddKph might help…
>>>
>>> Alex
>>>
>>> On Thu, 27 Dec 2018 at 18:59, Aseem Mohanty <ams.fwd at gmail.com> wrote:
>>>
>>>> Code: https://pastebin.com/0QfbPy5W
>>>> python 3.5
>>>>
>>>> What I am trying to do is allow declarative instance creation for some
>>>> classes.
>>>>
>>>> I have a metaclass that creates and initializes a target class declared
>>>> declaratively. The target may or may not have other bases. I am dynamically
>>>> setting up a base class for the target so that the declarative bit will
>>>> work. I am also wrapping the __init__ for the target with a super call so
>>>> that some necessary initialization in the base will happen.
>>>>
>>>> In the code you will see I have two other parent classes for the target
>>>> that all have similar sigs and my understanding was that a super call would
>>>> call all inits as long as they are co-operative .
>>>> However that does not seem to happen. The MRO is setup appropriately
>>>> but never bubbles up past the first init.
>>>>
>>>> Any help would be greatly appreciated.
>>>>
>>>> Thanks.
>>>> _______________________________________________
>>>> Baypiggies mailing list
>>>> Baypiggies at python.org
>>>> To change your subscription options or unsubscribe:
>>>> https://mail.python.org/mailman/listinfo/baypiggies
>>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20181227/d0eb4246/attachment-0001.html>


More information about the Baypiggies mailing list