How to add functions to a class dynamically based on some criteria.
For ex i have a class as follwos:
class A(object):
def __init__(self,type):
self.type = type
Now based on the ‘type’ value i want to add functions from either class B or class C to the class A.
For ex.
lass B(object):
def fun1(self):
print 'fun1'
def fun2(self):
print 'fun2'
class C(object):
def fun3(self):
print 'fun3'
def fun4(self):
print 'fun4'
If ‘type’ attribute value of class A is ‘B’ then dynamically add functions of class B (i.e. fun1 and fun2) to class A or else if ‘type’ attribute value is ‘C’ then add functions of class C to class A (i.e. fun3 and fun4) to class A, such that at further in my code i can access those functions as A.fun1() or A.fun3.
I am guessing that metaprograming might hekp me in this but i dont have a clue as to how to do it. Please guide.
Further i have to create objects of class A and be able to use these functions via those objects of class A.
a = class A(type='B')
a.fun1() #should work
Kindly help as i am not able to figure this out.
This is the part of my code.
class Group(object):
def __init__(self,type):
if type == 'source'
self.mgr = SManager(self) #grp_mgr
elif type == 'target'
self.mgr = TManager(self)
def __getattr__(self,name):
if hasattr(self.mgr,name):
return getattr(self.mgr,name)
return AttributeError
class SManager(object):
def __init__(self,groupobj):
self.some_list = []
self.groupobj = groupobj
def doProcess(self):
#this function accesses self.groupobj
pass
def get_valid_list(self):
#this function accesses self.groupobj
pass
But with this i am getting the following error.
if hasattr(self.mgr,name):
File "c:\workspace\project\dev.py", line 27, in __ge
tattr__
if hasattr(self.mgr,name):
File "c:\workspace\project\dev.py", line 27, in __ge
tattr__
if hasattr(self.mgr,name):
File "c:\workspace\project\dev.py", line 27, in __ge
tattr__
if hasattr(self.mgr,name):
File "c:\workspace\project\dev.py", line 27, in __ge
tattr__
if hasattr(self.mgr,name):
File "c:\workspace\project\dev.py", line 27, in __ge
tattr__
if hasattr(self.mgr,name):
RuntimeError: maximum recursion depth exceeded while calling a Python object
I am not able to figure out as to where i am making a mistake. Before incorporating this i tried this example with a simple snippet which worked but when i did the same in actual code it is throwing error.
Pls help
As I already told in a question comment: you probably don’t need metaclasses at all.
But since you’re not giving more information, this is how what you’re asking could be done with metaclasses:
That’s it, you’ll just need to inherit from
X.Example:
Edit: What you really need depends on how your actual code looks like.
What you described in your question is a dynamic inheritance, if that’s what you really need, then IMO a metaclass is the right tool and overriding __getattr__ it would be just a hack.
If you don’t want to build a whole metaclass you could build just use some kind of class factory:
I wouldn’t know which one of the two options looks better.