I've found myself in an unusual situation where I need to change the MRO of a class at runtime.
The code:
class A(object):
def __init__(self):
print self.__class__
print "__init__ A"
self.hello()
def hello(self):
print "A hello"
class B(A):
def __init__(self):
super(B, self).__init__()
print "__init__ B"
self.msg_str = "B"
self.hello()
def hello(self):
print "%s hello" % self.msg_str
a = A()
b = B()
As to be expected, this fails as the __init__
method of A (when called from B) calls B's hello
which attempts to access an attribute before it exists.
The issue is that I'm constrained in the changes I can make:
__init__
I did solve this conceptually, by changing the MRO at runtime. In brief, during B's __init__
, but before calling super __init__
, the MRO would be changed so that A would be searched for methods first, thereby calling A's hello
instead of B's (and therefore failing).
The issue is that MRO is read only (at class runtime).
Is there another way to implement this ? Or possibly a different solution altogether (that still respects the aforementioned constraints) ?
The Method Resolution Order (MRO) is the set of rules that construct the linearization. In the Python literature, the idiom "the MRO of C" is also used as a synonymous for the linearization of the class C.
Method Resolution Order(MRO) it denotes the way a programming language resolves a method or attribute. Python supports classes inheriting from other classes. The class being inherited is called the Parent or Superclass, while the class that inherits is called the Child or Subclass.
The Method Resolution Order (MRO) is the set of rules that construct the linearization. In the Python literature, the idiom "the MRO of C" is also used as a synonymous for the linearization of the class C. For instance, in the case of single inheritance hierarchy, if C is a subclass of C1, and C1 is a subclass of C2,...
This order is also called Linearization of a class and set of rules are called MRO (Method Resolution Order). While inheriting from another class, the interpreter needs a way to resolve the methods that are being called via an instance. Thus we need the method resolution order. For Example Attention geek!
You need to install a Cumulative Update (CU) to change the default R or Python language runtime version: SQL Server 2016: Services Pack (SP) 2 Cumulative Update (CU) 14 or later To download the latest Cumulative Update, see the Latest updates for Microsoft SQL Server.
The other provided answers are advisable if you are not bound by the constraints mentioned in the question. Otherwise, we need to take a journey into mro hacks and metaclass land.
After some reading, I discovered you can change the mro of a class, using a metaclass.
This however, is at class creation time, not at object creation time. Slight modification is necessary.
The metaclass provides the mro
method, which we overload, that is called during class creation (the metaclass' __new__
call) to produce the __mro__
attribute.
The __mro__
attribute is not a normal attribute, in that:
__new__
callHowever, it appears to be recalculated (using the mro
method) when a class' base is changed. This forms the basis of the hack.
In brief:
B
) is created using a metaclass (change_mro_meta
). This metaclass provides:
__mro__
attributechange_mro
) to control the mro behaviour As mentioned, modifying the mro of a class while in its __init__
is not thread safe.
The following may disturb some viewers. Viewer discretion is advised.
The hack:
class change_mro_meta(type):
def __new__(cls, cls_name, cls_bases, cls_dict):
out_cls = super(change_mro_meta, cls).__new__(cls, cls_name, cls_bases, cls_dict)
out_cls.change_mro = False
out_cls.hack_mro = classmethod(cls.hack_mro)
out_cls.fix_mro = classmethod(cls.fix_mro)
out_cls.recalc_mro = classmethod(cls.recalc_mro)
return out_cls
@staticmethod
def hack_mro(cls):
cls.change_mro = True
cls.recalc_mro()
@staticmethod
def fix_mro(cls):
cls.change_mro = False
cls.recalc_mro()
@staticmethod
def recalc_mro(cls):
# Changing a class' base causes __mro__ recalculation
cls.__bases__ = cls.__bases__ + tuple()
def mro(cls):
default_mro = super(change_mro_meta, cls).mro()
if hasattr(cls, "change_mro") and cls.change_mro:
return default_mro[1:2] + default_mro
else:
return default_mro
class A(object):
def __init__(self):
print "__init__ A"
self.hello()
def hello(self):
print "A hello"
class B(A):
__metaclass__ = change_mro_meta
def __init__(self):
self.hack_mro()
super(B, self).__init__()
self.fix_mro()
print "__init__ B"
self.msg_str = "B"
self.hello()
def hello(self):
print "%s hello" % self.msg_str
a = A()
b = B()
Some notes:
The hack_mro
, fix_mro
and recalc_mro
methods are staticmethods to the metaclass but classmethods to the class. It did this, instead of multiple inheritance, because I wanted to group the mro code together.
The mro
method itself returns the default ordinarily. Under the hack condition, it appends the second element of the default mro (the immediate parent class) to the mro, thereby causing the parent class to see its own methods first before the subclass'.
I'm unsure of the portability of this hack. Its been tested on 64bit CPython 2.7.3 running on Windows 7 64bit.
Don't worry, I'm sure this won't end up in production code somewhere.
There may be grander solutions but a simple option is to write class B defensively. For example:
class B(A):
def __init__(self):
super(B, self).__init__()
print "__init__ B"
self.msg_str = "B"
self.hello()
def hello(self):
if not hasattr(self, 'msg_str'):
A.hello(self)
return
print "%s hello" % self.msg_str
A good editor with regex capability could auto-insert appropriate if not hasattr(self, 'some_flag'):...
lines as the first lines of any methods in B.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With