C
crazychimp132
Greetings.
I am looking for a way to achieve method behavior for a class I
created. That is, it has a __call__ method, so can be called like a
function. But I also want it to be treated as a method when it appears
in a class body.
Eg.
class foo:
def __call__(self, inst): pass
class bar:
meth = foo()
such that bar().meth() will not raise an exception for too few
arguments (because the inst argument in foo.__call__ is implicitly set
to the bar instance). I know this has to do with writing the __get__
method of foo, but I am wondering if there is perhaps some class I can
just inherit from to get the proper __get__, which behaves identically
to that of regular Python functions. The need for this arises out of
the implementation of a function decorator as a class.
Thanks.
I am looking for a way to achieve method behavior for a class I
created. That is, it has a __call__ method, so can be called like a
function. But I also want it to be treated as a method when it appears
in a class body.
Eg.
class foo:
def __call__(self, inst): pass
class bar:
meth = foo()
such that bar().meth() will not raise an exception for too few
arguments (because the inst argument in foo.__call__ is implicitly set
to the bar instance). I know this has to do with writing the __get__
method of foo, but I am wondering if there is perhaps some class I can
just inherit from to get the proper __get__, which behaves identically
to that of regular Python functions. The need for this arises out of
the implementation of a function decorator as a class.
Thanks.