473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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>
Sep 23 '08 #1
5 1665
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...

Sep 23 '08 #2
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
Sep 23 '08 #3
On Tue, 23 Sep 2008 15:23:35 +1000, Tom Harris <ce********@gmail.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,
http://webpages.charter.net/curryfans/peter/nature.py:

boltzmanns_constant = 1.380622e-16 * erg / k
stefan_boltzmann_constant = 5.66961e-5 * erg/s/cm/cm/k/k/k/k
gravitational_constant = 6.6732e-8 * erg*cm/g/g

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

--
To email me, substitute nowhere->spamcop, invalid->net.
Sep 23 '08 #4
Peter Pearson wrote:
On Tue, 23 Sep 2008 15:23:35 +1000, Tom Harris <ce********@gmail.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,
http://webpages.charter.net/curryfans/peter/nature.py:

boltzmanns_constant = 1.380622e-16 * erg / k
stefan_boltzmann_constant = 5.66961e-5 * erg/s/cm/cm/k/k/k/k
gravitational_constant = 6.6732e-8 * erg*cm/g/g

I would like to reveal more details with, e.g.,
help( gravitational_constant ) . . . and maybe then I could
use shorter names.
gravitational_constant = g = 6.6732e-8 * erg*cm/g/g
....

Sep 23 '08 #5
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(value, doc=''):
def getter(self):
return value
return property(getter, None, None, doc)
class Foo(object):
x = make_const(1.234, 'a special number')
The only gotcha is that while help(Foo.x) works, help(Foo().x) does not.
--
Steven
Sep 24 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: Bob Parnes | last post by:
In its default configuration, my version of pylint (0.5.0) sets the maximum number of class attributes at 7. This seems low to me, but I can see how an excessive number might make maintenance more...
0
by: harold fellermann | last post by:
Hello, I just posted this question with a wrong subject... So here again with a better one. I am working on a C extension module that implements a bunch of classes. Everything works fine so...
9
by: Sandeep Sharma | last post by:
For many years I have been following the convention of naming all class attributes with a leading underscore. This enables me to quickly identify the class attributes when I encounter them in the...
2
by: Ewald R. de Wit | last post by:
I'm running into a something unexpected for a new-style class that has both a class attribute and __slots__ defined. If the name of the class attribute also exists in __slots__, Python throws an...
1
by: dasmart | last post by:
I'm new to the .NET world and am in the process of learning about VB.NET classes. I've tried to find information on class attributes (overview, when/why to use them, etc.) but every link from MSDN...
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
2
by: james_027 | last post by:
hi everyone, I am now in chapter 5 of Dive Into Python and I have some question about it. From what I understand in the book is you define class attributes & data attributes like this in python...
4
by: Wilbert Berendsen | last post by:
Hi, is it possible to manipulate class attributes from within a decorator while the class is being defined? I want to register methods with some additional values in a class attribute. But I...
0
by: Terry Reedy | last post by:
John Hanks wrote: Newsreaders do not typically have line counters. I presume you are referring to def setSolution(self, val): self.solved = True self.solutions = set((val,))...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.