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

Steven D'Aprano steve at pearwood.info
Thu Dec 10 04:33:31 EST 2015


On Thu, Dec 10, 2015 at 09:58:37AM +0100, 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.

I must admit I am not familiar with the terminology "Lift" in this 
context, so I may have missed something. But does this example help?


py> class A(object):
...     def spam(self):
...             return "spam"
...
py> class B(A):
...     def spam(self):
...             result = super(B, self).spam()
...             return " ".join([result]*3)
...
py> a = A()
py> a.spam()
'spam'
py> a.__class__ = B
py> a.spam()
'spam spam spam'
py> type(a)
<class '__main__.B'>




-- 
Steve


More information about the Python-ideas mailing list