class Callable:
"""A callable."""
def __call__(*args):
return True
class Foo:
a = staticmethod(Callable())
b = classmethod(Callable())
when I invoke help(Foo) I get the docstring of the Callable class for the class method but not the static method?
Help on class Foo in module foo:
class Foo(builtins.object)
| Class methods defined here:
|
| b = <bound method ? of <class 'foo.Foo'>>
| A callable.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| a = <foo.Callable object>
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
How would you go about defining the docstrings for static and class methods defined in this form? What about typing annotations?
>>> class Callable:
... """A callable."""
... def __call__(*args):
... return True
...
>>>
>>> class Foo:
... a = staticmethod(Callable())
... b = classmethod(Callable())
...
>>> Foo().a.__doc__
'A callable.'
>>> Foo().b.__doc__
'A callable.'
>>>
It’s just that the help() function doesn’t think to look for a docstring because a is neither a method nor a function (nor a class), but a plain old object that happens to be callable.
What I consider funny is that the static methods and class methods are treated differently. And, while the method is neither a function or a method, it is a staticmethod object, thus it is clearly marked as not being a plain old object that happens to be callable.
Of course, you are right. I was tricked by the fact that staticmethod() returns a staticmethod object, but the wrapping is removed when the class definition is “assembled”. Playing around a bit I found another thing that I found surprising at a first sight, but that makes completely sense after thinking about it for a few moments: