Often times you need to write wrappers or proxies that are required to support "forwarding functions", i.e. functions that just forward requests to similarly named functions, or do something parametrised by function name. The tedious way to do it is something like this:
class C
(
Read more... )
Comments 6
Reply
Reply
>>> class Person(object):
... def __init__(self, name):
... self.name = name
...
>>> joe = Person('Joe')
>>> joe.fullName = lambda : None #function on joe object. No way to access self
>>> print joe.fullName()
None
>>> del joe.fullName #Lets delete that function
>>> Person.fullName = lambda self: self.name #Now adding function on Person
>>> print joe.fullName() #This is a bound method to joe. No need to pass self
Joe
>>>
Reply
class C:
@staticmethod
def gen_func(name):
return (lambda self, arg: name + arg)
def __init__(self):
pass
fnames = ['a', 'b', 'c']
for fname in fnames:
C.__dict__[fname] = C.gen_func(fname)
################
# Let's try it out
################
if __name__ == '__main__':
a_c = C()
print a_c.a('a')
print a_c.b('b')
print a_c.c('c')
Reply
Leave a comment