This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: classmethod, staticmethod: expose wrapped function
Type: enhancement Stage:
Components: Versions: Python 3.2, Python 2.7
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: amaury.forgeotdarc Nosy List: amaury.forgeotdarc, gsakkis, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2009-05-09 21:05 by gsakkis, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
staticmethod_func.patch amaury.forgeotdarc, 2009-05-20 09:29
Messages (6)
msg87511 - (view) Author: George Sakkis (gsakkis) Date: 2009-05-09 21:05
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
msg87839 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-05-15 20:48
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.
msg88101 - (view) Author: George Sakkis (gsakkis) Date: 2009-05-19 23:25
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".
msg88102 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-05-20 01:27
I am all for exposing the __func__ directly but don't immediately see
how to do it.
msg88109 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-05-20 09:29
Is there a difficulty I did not see? Here is a patch.
msg88498 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-05-29 05:00
r73005 and r73006
Date User Action Args
2022-04-11 14:56:48adminsetgithub: 50232
2009-05-29 05:00:53rhettingersetstatus: open -> closed

messages: + msg88498
2009-05-20 17:48:15rhettingersetassignee: amaury.forgeotdarc
resolution: accepted
2009-05-20 09:29:19amaury.forgeotdarcsetfiles: + staticmethod_func.patch

nosy: + amaury.forgeotdarc
messages: + msg88109

keywords: + patch
2009-05-20 01:27:34rhettingersetassignee: rhettinger -> (no value)
messages: + msg88102
2009-05-19 23:25:09gsakkissetmessages: + msg88101
2009-05-15 20:48:53terry.reedysetnosy: + terry.reedy

messages: + msg87839
versions: + Python 3.2
2009-05-11 20:37:57rhettingersetassignee: rhettinger

nosy: + rhettinger
2009-05-09 21:05:02gsakkiscreate