I am trying to understand the difference of returning a lambda vs a method in a decorator below. The calling object instance "o" is missing and not passed by the decorator when returning m.fm. The decorator will pass the instance while returning a lambda function. Is it possible to pass the calling object by returning a method in decorator?
def deco(*type):
def wrapper(func):
m = M()
# return lambda callingobj, *args: m.fm(callingobj, *args)
return m.fm
return wrapper
class M(object):
def fm(self, callingobj, *args):
print(f'self_ {callingobj}, args {args}')
class O(object):
@deco('int')
def fo(self, *args):
print(f'{args}')
o = O()
o.fo(1, 2, 3)
Output:
with
return lambda callingobj, *args: m.fm(callingobj, *args)
callingobj <__main__.O object at 0x000002453BE95760>, args (1, 2, 3)
with
return m.fm
callingobj 1, args (2, 3)