You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
classmethod, staticmethod: expose wrapped function #50232
It would be nice if classmethod/staticmethod exposed the wrapped
function as a read-only attribute/property. Currently the function can
be retrieved indirectly but it's obscure (and perhaps not always
correct, I'm not sure):
In [147]: def f():pass
.....:
In [148]: c = classmethod(f)
In [149]: s = staticmethod(f)
In [150]: c.__get__(1).im_func is f
Out[150]: True
Having to bind and unbind a classmethod to get at the function is
definitely a bit weird, and also susceptible to changing implementation,
but do you have a practical use case to motivate the effort needed?
In Py3, '__func__' replaced 'im_func', and that is the name to use if
the attribute is added. A change in 3.2 is at least as likely as a
change in 2.7.
gsakkis commented on May 19, 2009
gsakkismannequin
MannequinAuthor
More actions
I don't remember the exact use case but it had to do with making a
decorator robust enough to work for different kinds of callables (and a
few common non-callables such as classmethod/staticmethod). It's not a
show stopper by any means but I thought it would be easy (if not
trivial) to implement, and regardless of actual use cases I don't see a
reason to keep the function "hidden".
Activity
gsakkis commentedon May 9, 2009
It would be nice if classmethod/staticmethod exposed the wrapped
function as a read-only attribute/property. Currently the function can
be retrieved indirectly but it's obscure (and perhaps not always
correct, I'm not sure):
In [147]: def f():pass
.....:
In [148]: c = classmethod(f)
In [149]: s = staticmethod(f)
In [150]: c.__get__(1).im_func is f
Out[150]: True
In [151]: s.__get__(1) is f
Out[151]: True
terryjreedy commentedon May 15, 2009
Having to bind and unbind a classmethod to get at the function is
definitely a bit weird, and also susceptible to changing implementation,
but do you have a practical use case to motivate the effort needed?
In Py3, '__func__' replaced 'im_func', and that is the name to use if
the attribute is added. A change in 3.2 is at least as likely as a
change in 2.7.
gsakkis commentedon May 19, 2009
I don't remember the exact use case but it had to do with making a
decorator robust enough to work for different kinds of callables (and a
few common non-callables such as classmethod/staticmethod). It's not a
show stopper by any means but I thought it would be easy (if not
trivial) to implement, and regardless of actual use cases I don't see a
reason to keep the function "hidden".
rhettinger commentedon May 20, 2009
I am all for exposing the __func__ directly but don't immediately see
how to do it.
amauryfa commentedon May 20, 2009
Is there a difficulty I did not see? Here is a patch.
rhettinger commentedon May 29, 2009
r73005 and r73006