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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to