[Python-ideas] generic Liftable abc-mixin breaks at MRO

Stephan Sahm Stephan.Sahm at gmx.de
Thu Dec 10 10:45:54 EST 2015


Does anyone know how to handle this MRO-error or how to circumvent it? To
me, the error seems unneeded in this case

On 10 December 2015 at 10:52, Stephan Sahm <Stephan.Sahm at gmx.de> wrote:

> Thanks for asking, I am glad to explain the idea behind Liftable:
>
> My first implementation of *lift* just checked whether the class has an
> initialize method (and not being of type Liftable. However then B must look
> like so:
>
> class B(A):
>     def __init__(self, b, a):
>         super(B, self).__init__(a)
>         self.__initialize__(b)
>
>     def __initialize__(self, b):
>         self.b = b
>
> this *__init__* method is generic for all Liftable-types in my sense, so
> I wanted to factorize it out, where the first guess I had was to use an
> abc-mixin.
>
> So one main point is that I want to make further initializations to my
> lifted instance which make it a truly valid instance of the new type.
>
> best,
> Stephan
>
> On 10 December 2015 at 10:44, M.-A. Lemburg <mal at egenix.com> wrote:
>
>> On 10.12.2015 10:35, Stephan Sahm wrote:
>> > thanks for the fast response
>> > in fact, I do use this already, however more generically:
>> >
>> > def lift(self, new_class, **kwargs):
>> > ​    ​
>> > ...
>> > ​    ​
>> > self.__class__ = new_class
>> >     ...
>>
>> Hmm, then I don't understand why you need a meta class
>> for this. Could you explain the requirements that led up to
>> needing a meta class ?
>>
>> If all you want to do is let the lift() know about
>> a certain property of a class to do it's thing, an
>> attribute or perhaps an empty base class would do the
>> trick, e.g.
>>
>> class A:
>>     __liftable__ = True
>>
>> or perhaps:
>>
>> class A:
>>     def lift(self, ...):
>>         # lift operation goes here
>>
>> > On 10 December 2015 at 10:33, M.-A. Lemburg <mal at egenix.com> wrote:
>> >
>> >> On 10.12.2015 09:58, Stephan Sahm wrote:
>> >>> Dear all,
>> >>>
>> >>> I think I found a crucial usecase where the standard MRO does not work
>> >> out.
>> >>> I would appreciate your help to still solve this usecase, or might MRO
>> >> even
>> >>> be adapted?
>> >>>
>> >>> The idea is to build a generic Lift-type which I call this way because
>> >> the
>> >>> derived classes should be able to easily lift from subclasses. So for
>> >>> example if I have an instance *a* from *class A* and a *class B(A)* I
>> >> want
>> >>> to make *a* an instance of *B* in a straightforward way.
>> >>
>> >> Why don't you use:
>> >>
>> >> a.__class__ = B
>> >>
>> >> ?
>> >>
>> >>>>> class A: pass
>> >> ...
>> >>>>> class B(A): pass
>> >> ...
>> >>>>> a = A()
>> >>>>> a
>> >> <__main__.A instance at 0x7f0bf5ccdc68>
>> >>>>> a.__class__ = B
>> >>>>> a
>> >> <__main__.B instance at 0x7f0bf5ccdc68>
>> >>>>>
>> >>
>> >> --
>> >> Marc-Andre Lemburg
>> >> eGenix.com
>> >>
>> >> Professional Python Services directly from the Experts (#1, Dec 10
>> 2015)
>> >>>>> Python Projects, Coaching and Consulting ...
>> http://www.egenix.com/
>> >>>>> Python Database Interfaces ...
>> http://products.egenix.com/
>> >>>>> Plone/Zope Database Interfaces ...
>> http://zope.egenix.com/
>> >>
>> ________________________________________________________________________
>> >>
>> >> ::: We implement business ideas - efficiently in both time and costs
>> :::
>> >>
>> >>    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>> >>     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>> >>            Registered at Amtsgericht Duesseldorf: HRB 46611
>> >>                http://www.egenix.com/company/contact/
>> >>                       http://www.malemburg.com/
>> >>
>> >>
>> >
>> >
>> >
>> > _______________________________________________
>> > Python-ideas mailing list
>> > Python-ideas at python.org
>> > https://mail.python.org/mailman/listinfo/python-ideas
>> > Code of Conduct: http://python.org/psf/codeofconduct/
>> >
>>
>> --
>> Marc-Andre Lemburg
>> eGenix.com
>>
>> Professional Python Services directly from the Experts (#1, Dec 10 2015)
>> >>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>> >>> Python Database Interfaces ...           http://products.egenix.com/
>> >>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
>> ________________________________________________________________________
>>
>> ::: We implement business ideas - efficiently in both time and costs :::
>>
>>    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>>     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>>            Registered at Amtsgericht Duesseldorf: HRB 46611
>>                http://www.egenix.com/company/contact/
>>                       http://www.malemburg.com/
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151210/758ebc43/attachment.html>


More information about the Python-ideas mailing list