Docstrings for class attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Harris

    Docstrings for class attributes

    Greetings,

    I want to have a class as a container for a bunch of symbolic names
    for integers, eg:

    class Constants:
    FOO = 1
    BAR = 2

    Except that I would like to attach a docstring text to the constants,
    so that help(Constants. FOO) will print some arbitrary string. Sort of
    a very limited implementation of PEP 224. The only solution that I can
    see is to subclass int.__new__(), since once I have an int all it's
    attributes are immutable.

    --

    Tom Harris <celephicus(AT) gmail(DOT)com>
  • Bruno Desthuilliers

    #2
    Re: Docstrings for class attributes

    Tom Harris a écrit :
    Greetings,
    >
    I want to have a class as a container for a bunch of symbolic names
    for integers, eg:
    >
    class Constants:
    FOO = 1
    BAR = 2
    Do you have a reason to stuff them in a class ? Usually, putting them at
    the top level of a module is quite enough...
    Except that I would like to attach a docstring text to the constants,
    so that help(Constants. FOO) will print some arbitrary string.
    You can document them in the module or class docstring...

    Comment

    • Diez B. Roggisch

      #3
      Re: Docstrings for class attributes

      Tom Harris wrote:
      Greetings,
      >
      I want to have a class as a container for a bunch of symbolic names
      for integers, eg:
      >
      class Constants:
      FOO = 1
      BAR = 2
      >
      Except that I would like to attach a docstring text to the constants,
      so that help(Constants. FOO) will print some arbitrary string. Sort of
      a very limited implementation of PEP 224. The only solution that I can
      see is to subclass int.__new__(), since once I have an int all it's
      attributes are immutable.
      Epydoc interprets strings like this as doc-strings:

      """
      FOO is not a bar
      """
      FOO = "foo"


      However, it won't get recognized by help of course.

      Diez

      Comment

      • Peter Pearson

        #4
        Re: Docstrings for class attributes

        On Tue, 23 Sep 2008 15:23:35 +1000, Tom Harris <celephicus@gma il.comwrote:
        >
        I want to have a class as a container for a bunch of symbolic names
        for integers, eg:
        >
        class Constants:
        FOO = 1
        BAR = 2
        >
        Except that I would like to attach a docstring text to the constants,
        so that help(Constants. FOO) will print some arbitrary string.
        [snip]

        Commiserating, not helping: I have a similar problem with a module
        that holds values of physical constants,


        boltzmanns_cons tant = 1.380622e-16 * erg / k
        stefan_boltzman n_constant = 5.66961e-5 * erg/s/cm/cm/k/k/k/k
        gravitational_c onstant = 6.6732e-8 * erg*cm/g/g

        I would like to reveal more details with, e.g.,
        help( gravitational_c onstant ) . . . and maybe then I could
        use shorter names.

        --
        To email me, substitute nowhere->spamcop, invalid->net.

        Comment

        • Terry Reedy

          #5
          Re: Docstrings for class attributes

          Peter Pearson wrote:
          On Tue, 23 Sep 2008 15:23:35 +1000, Tom Harris <celephicus@gma il.comwrote:
          >I want to have a class as a container for a bunch of symbolic names
          >for integers, eg:
          >>
          >class Constants:
          > FOO = 1
          > BAR = 2
          >>
          >Except that I would like to attach a docstring text to the constants,
          >so that help(Constants. FOO) will print some arbitrary string.
          [snip]
          >
          Commiserating, not helping: I have a similar problem with a module
          that holds values of physical constants,

          >
          boltzmanns_cons tant = 1.380622e-16 * erg / k
          stefan_boltzman n_constant = 5.66961e-5 * erg/s/cm/cm/k/k/k/k
          gravitational_c onstant = 6.6732e-8 * erg*cm/g/g
          >
          I would like to reveal more details with, e.g.,
          help( gravitational_c onstant ) . . . and maybe then I could
          use shorter names.
          gravitational_c onstant = g = 6.6732e-8 * erg*cm/g/g
          ....

          Comment

          • Steven D'Aprano

            #6
            Re: Docstrings for class attributes

            On Tue, 23 Sep 2008 15:23:35 +1000, Tom Harris wrote:
            Greetings,
            >
            I want to have a class as a container for a bunch of symbolic names for
            integers, eg:
            >
            class Constants:
            FOO = 1
            BAR = 2
            >
            Except that I would like to attach a docstring text to the constants, so
            that help(Constants. FOO) will print some arbitrary string. Sort of a
            very limited implementation of PEP 224. The only solution that I can see
            is to subclass int.__new__(), since once I have an int all it's
            attributes are immutable.


            Others have suggested solutions, which may be better, but for
            completeness consider using properties:

            def make_const(valu e, doc=''):
            def getter(self):
            return value
            return property(getter , None, None, doc)


            class Foo(object):
            x = make_const(1.23 4, 'a special number')


            The only gotcha is that while help(Foo.x) works, help(Foo().x) does not.


            --
            Steven

            Comment

            Working...