pydoc in 2.1: Missing static methods

Tim Peters tim.one at home.com
Mon Oct 8 15:40:20 EDT 2001


[F. GEIGER]
> I use pydoc of ActivePython 2.1.212 and am pretty happy with it,
> except when it comes to "static methods":  Such methods are not shown by
> pydoc!

That's because they're not methods:  they're data, and pydoc in 2.1 or
before never shows class data.

> I use Alex Martelli's pattern to define the static methods (aka
> class-methods, see AS' Python Cookbook):
>
> class HcForm:
>
> <snipped>
>
>    def restoreFromSession(name, session, request):
>       try:
>          form = pickle.loads(session.value(name))
>       except KeyError:
>          return None
>       form._request = request
>       form.__processRequest()
>       return form
>
>    restoreFromSession = static(restoreFromSession)
>
> I guess pydoc misses "self",

pydoc doesn't look for "self".  After the above, HcForm.restoreFromSession
is simply a data object (an instance of class static), and doesn't mean any
more to pydoc than e.g.

    restoreFromSession = int

or

    restoreFromSession = 42

or

    restoreFromSession = AnyRandomClass()

would mean.

In 2.2, pydoc:

1. Does show data objects in class dictionaries.

2. Shows static methods (created by the new builtin staticmethod()
   function) in a "static methods" subsection.

> ...
> I don't know how pydoc should label this method. Should it prefix it with
> "static"? Or should it simply list it with the other methods?
>
> static() is defined like so:
>
> class static:
> ...

Here's a 2.2 example:

class Example(object):
    def statmeth():
        "A 2.2 static method example."
        return 666

    statmeth = staticmethod(statmeth)

    def ordinary(me):
        "An ordinary method."
        return me

    dataobject = 666

$ python
...
>>> import example
>>> help(example.Example)
Help on class Example in module example:

class Example(__builtin__.object)
 |  Methods defined here:
 |
 |  ordinary(me)
 |      An ordinary method.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  statmeth()
 |      A 2.2 static method example.
 |
 |  ----------------------------------------------------------------------
 |  Data and non-method functions defined here:
 |
 |  __dict__ = {'__dict__': <attribute '__dict__' of 'Example' objects>,'
 |  ...
 |
 |  __module__ = 'example'
 |
 |  __weakref__ = <member '__weakref__' of 'Example' objects>
 |
 |  dataobject = 666
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from __builtin__.object:
 |
 |  __delattr__(...)
 |      x.__delattr__('name') <==> del x.name
 |
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |
 |  __init__(...)
 |      x.__init__(...) initializes x; see x.__class__.__doc__ for signature
 |
 |  __reduce__(...)
 |      helper for pickle
 |
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |
 |  __setattr__(...)
 |      x.__setattr__('name', value) <==> x.name = value
 |
 |  __str__(...)
 |      x.__str__() <==> str(x)
 |
 |  ----------------------------------------------------------------------
 |  Data and non-method functions inherited from __builtin__.object:
 |
 |  __class__ = <type 'type'>
 |      the object's class
 |
 |  __new__ = <built-in method __new__ of type object at 0x1E0B6F08>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

>>>

2.2 class methods (created by the new builtin classmethod() function) are
also shown in their own subsection.

Before 2.2, you're out of luck, short of perhaps changing inspect.py and
pydoc.py to recognize and special-case the specific class name "static"
(provided that's the name you always use).  This gets pretty involved,
though, as pydoc ignores class data objects entirely in 2.1, and teaching it
not to requires structural changes to the code (i.e., it's not a one-liner
patch, it's complicated).

voice-of-painful-experience-ly y'rs  - tim





More information about the Python-list mailing list