property docstrings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darren Dale

    #1

    property docstrings

    I am trying to work with properties, using python 2.4.2. I can't get the
    docstrings to work, can someone suggest what I'm doing wrong? I think the
    following script should print "This is the doc string.", but instead it
    prints:

    "float(x) -> floating point number

    Convert a string or number to a floating point number, if possible."

    Thanks,
    Darren



    class Example(object) :
    _myattr = 0.0

    @apply
    def myattr():
    doc = """This is the doc string."""
    def fget(self):
    return self._myattr
    def fset(self, value):
    self._myattr = value
    def fdel(self):
    del self._myattr
    return property(**loca ls())

    _foo = 1.0
    def foo():
    doc = """This is the doc string."""
    def fget(self):
    """This is the doc string."""
    return self._foo
    def fset(self, value):
    """This is the doc string."""
    self._foo = value
    def fdel(self):
    """This is the doc string."""
    del self._foo
    return locals()
    foo = property(**foo( ))

    _bar = 1.0
    doc = """This is the doc string."""
    def get_bar(self):
    return self._bar
    def set_bar(self, value):
    self._bar = value
    def del_bar(self):
    del self._bar
    bar = property(get_ba r, set_bar, del_bar, doc)

    a=Example()
    print 'myattr docstring:\n', a.myattr.__doc_ _
    print 'foo docstring:\n', a.foo.__doc__
    print 'bar docstring:\n', a.bar.__doc__

  • Ziga Seilnacht

    #2
    Re: property docstrings

    Darren Dale wrote:[color=blue]
    > I am trying to work with properties, using python 2.4.2. I can't get the
    > docstrings to work, can someone suggest what I'm doing wrong? I think the
    > following script should print "This is the doc string.", but instead it
    > prints:
    >
    > "float(x) -> floating point number
    >
    > Convert a string or number to a floating point number, if possible."
    >
    > Thanks,
    > Darren[/color]

    <snip>
    [color=blue]
    > a=Example()
    > print 'myattr docstring:\n', a.myattr.__doc_ _
    > print 'foo docstring:\n', a.foo.__doc__
    > print 'bar docstring:\n', a.bar.__doc__[/color]

    change this part to:

    print 'myattr docstring:\n', Example.myattr. __doc__
    print 'foo docstring:\n', Example.foo.__d oc__
    print 'bar docstring:\n', Example.bar.__d oc__

    What happens is that when property is accessed
    from an instance, it returns whatever the fget
    function returns, and the __doc__ attribute
    is then looked up on that object.

    To get to the actual property object (and its
    __doc__ attribute) you have to access it from
    a class.

    Ziga

    Comment

    Working...