doc strings of properties

Peter Dobcsanyi peter at designtheory.org
Fri Feb 27 09:54:23 EST 2004


Hi,

I reworked a few methods of a class using properties but then realized
that the online help in interactive sessions would not work as I
expected. The reason is the way how doc strings of properties is treated.
Here is an example to illuminate the problem:

    >>> class Foo(object):
    ...     def __init__(self):
    ...         self.a = 2
    ...         self.b = 3
    ...     def set(self, x, y):
    ...         'Set a and b.'
    ...         self.a = x
    ...         self.b = y
    ...     def _axb(self):
    ...         return self.a * self.b
    ...     axb = property(_axb, doc='Return current product a*b.')
    ... 
    >>> 

    ()
    >>> print f.a, f.b, f.axb
    2 3 6
    >>> 
    >>> print f.set.__doc__
    Set a and b.
    >>> 
    >>> print f.axb.__doc__
    int(x[, base]) -> integer

    Convert a string or number to an integer, if possible.  A floating point
    argument will be truncated towards zero (this does not include a string
    representation of a floating point number!)  When converting a string,
    use
    the optional base.  It is an error to supply a base when converting a
    non-string. If the argument is outside the integer range a long object
    will be returned instead.
    >>>
    >>> print Foo.axb.__doc__
    Return current product a*b.
    >>> 

I have a vague idea why this is happening, nevertheless find it a bit
inconsistent compared to doc strings of standard methods. Things are not
too helpful with help() either.

    >>> help(f.set)
    Help on method set:

    set(self, x, y) method of __main__.Foo instance
	Set a and b.

    >>> help(Foo.set)
    Help on method set:

    set(self, x, y) unbound __main__.Foo method
	Set a and b.

    >>> help(f.axb)
    Help on int:

    6

    >>> help(Foo.axb)
    Help on property:

    <property object>

    >>>

Although, help on the class gives what one would expect:

    >>> help(Foo)
    Help on class Foo in module __main__:

    class Foo(__builtin__.object)
     |  Methods defined here:
     |  
     |  __init__(self)
     |  
     |  set(self, x, y)
     |      Set a and b.
     |  
     |
    ----------------------------------------------------------------------
     |  Properties defined here:
     |  
     |  axb
     |      Return current product a*b.
     |  
     |      <get> = _axb(self)
     |  
     ...

Is there a way to work around this problem or I am missing something
here.

Thanks,
    Peter



More information about the Python-list mailing list