I
iu2
Hi
I'm trying to make a method call automatically to its super using this
syntax:
class A:
chained = ['pr']
def pr(self):
print 'Hello from A'
class B(A):
def pr(self):
print 'Hello from B'
chain(B, A)
b = B()
b.pr()
b.pr() will print
Hello from B
Hello from A
I'm doing it using the 'chained' attribute in class A, and with this
function:
def chain(cls, sup):
for m in dir(cls):
if callable(getattr(cls, m)) and m in cls.chained:
cm = getattr(cls, m)
def m2(*p):
cm(*p)
return getattr(sup, m)(*p)
setattr(cls, m, m2)
return cls
which seeks for all 'chained' methods and adjusts them accordingly.
(had there been class decorators the syntax would have been simpler,
something like
class A:
@make_chained
def pr():
print 'Hello from A'
@chained
class B:
def pr():
print 'Hello from B'
)
My problem is this: Currently I pass the base class to 'chain' -
chain(B, A)
I prefer to write
chain(B)
and let 'chain' use the super of B.
So:
def chain(cls):
for m in dir(cls):
if callable(getattr(cls, m)) and m in cls.chained:
print 'chaning', cls, m
cm = getattr(cls, m)
def m2(*p):
cm(*p)
return getattr(super(cls), m)(*p)
setattr(cls, m, m2)
This is probably wrong because I don't give the object instance to
super (I don't have it!) and I also get the error
TypeError: super() argument 1 must be type, not classobj
Can you please help me with this?
Thanks
iu2
I'm trying to make a method call automatically to its super using this
syntax:
class A:
chained = ['pr']
def pr(self):
print 'Hello from A'
class B(A):
def pr(self):
print 'Hello from B'
chain(B, A)
b = B()
b.pr()
b.pr() will print
Hello from B
Hello from A
I'm doing it using the 'chained' attribute in class A, and with this
function:
def chain(cls, sup):
for m in dir(cls):
if callable(getattr(cls, m)) and m in cls.chained:
cm = getattr(cls, m)
def m2(*p):
cm(*p)
return getattr(sup, m)(*p)
setattr(cls, m, m2)
return cls
which seeks for all 'chained' methods and adjusts them accordingly.
(had there been class decorators the syntax would have been simpler,
something like
class A:
@make_chained
def pr():
print 'Hello from A'
@chained
class B:
def pr():
print 'Hello from B'
)
My problem is this: Currently I pass the base class to 'chain' -
chain(B, A)
I prefer to write
chain(B)
and let 'chain' use the super of B.
So:
def chain(cls):
for m in dir(cls):
if callable(getattr(cls, m)) and m in cls.chained:
print 'chaning', cls, m
cm = getattr(cls, m)
def m2(*p):
cm(*p)
return getattr(super(cls), m)(*p)
setattr(cls, m, m2)
This is probably wrong because I don't give the object instance to
super (I don't have it!) and I also get the error
TypeError: super() argument 1 must be type, not classobj
Can you please help me with this?
Thanks
iu2