I have a class A
:
class A(object):
a = 'a'
def __init__(self):
self.b = 'b'
def foo(self):
print A.a
print self.b
I just wondering how to construct this class by meta programming.
I have tried:
def Init(self):
self.b = 'b'
def Foo(self):
print A_CLASS.a
print self.b
A_CLASS = type('A_CLASS',(object,),{'a':'a','__init__':Init,'foo':Foo})
myObj = A_CLASS()
myObj.foo()
It's works, but still have some problem, that is, I have to use A_CLASS
as the name of the class, if I use B_CLASS
, it doesn't work anymore:
B_CLASS = type('A_CLASS',(object,),{'a':'a','__init__':Init,'foo':Foo})
myObj = B_CLASS()
myObj.foo() #Error ocurred
If there is a way to construct such a class, where the name of the variable 'X_CLASS' is not relevant at all?