[Baypiggies] Python3 Metaclass, inheritance and MRO question

Aseem Mohanty ams.fwd at gmail.com
Fri Dec 28 00:27:46 EST 2018


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/b68cad14/attachment.html>


More information about the Baypiggies mailing list