summaryrefslogtreecommitdiff
path: root/Objects/funcobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-12-17 02:53:53 +0000
committerGuido van Rossum <guido@python.org>2001-12-17 02:53:53 +0000
commit9564b5511d04e17ca132411bb1df3731e8366a49 (patch)
tree76aa8bb3a4ab910255b85b82ebb819a51c461b9d /Objects/funcobject.c
parent3e197aab9c8ba0017a8cfb4fa426d4c12989f151 (diff)
downloadcpython-9564b5511d04e17ca132411bb1df3731e8366a49.tar.gz
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Montanaro) (With minor adjustments.)
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r--Objects/funcobject.c43
1 files changed, 41 insertions, 2 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index ee016df4b6..6d7f29da78 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -491,6 +491,27 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds)
return 0;
}
+static char classmethod_doc[] =
+"classmethod(function) -> method\n\
+\n\
+Convert a function to be a class method.\n\
+\n\
+A class method receives the class as implicit first argument,\n\
+just like an instance method receives the instance.\n\
+To declare a class method, use this idiom:\n\
+\n\
+ class C:\n\
+ def f(cls, arg1, arg2, ...): ...\n\
+ f = classmethod(f)\n\
+\n\
+It can be called either on the class (e.g. C.f()) or on an instance\n\
+(e.g. C().f()). The instance is ignored except for its class.\n\
+If a class method is called for a derived class, the derived class\n\
+object is passed as the implied first argument.\n\
+
+Class methods are different than C++ or Java static methods.\n\
+If you want those, see the staticmethod builtin.";
+
PyTypeObject PyClassMethod_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
@@ -513,7 +534,7 @@ PyTypeObject PyClassMethod_Type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
+ classmethod_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
@@ -602,6 +623,24 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds)
return 0;
}
+static char staticmethod_doc[] =
+"staticmethod(function) -> method\n\
+\n\
+Convert a function to be a static method.\n\
+\n\
+A static method does not receive an implicit first argument.\n\
+To declare a static method, use this idiom:\n\
+\n\
+ class C:\n\
+ def f(arg1, arg2, ...): ...\n\
+ f = staticmethod(f)\n\
+\n\
+It can be called either on the class (e.g. C.f()) or on an instance\n\
+(e.g. C().f()). The instance is ignored except for its class.\n\
+\n\
+Static methods in Python are similar to those found in Java or C++.\n\
+For a more advanced concept, see the classmethod builtin.";
+
PyTypeObject PyStaticMethod_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
@@ -624,7 +663,7 @@ PyTypeObject PyStaticMethod_Type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
+ staticmethod_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */