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> 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...
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
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.
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
....
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,))...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
| |