Constants in Python

Brent W. Hughes brent.hughes at comcast.net
Tue Sep 7 01:06:58 EDT 2004


Warning:  Newbie at work!

I was planning to write some code something like this:

class CTree:
    LEAFSIZE = 4
    LeafNode = [None] * LEAFSIZE
    KEY,DATA,LEFT,RIGHT = range(LEAFSIZE)
#---------------------------------------------------
    def Add(self,Node,SubTree):
        Key1 = Node[KEY]
...

But then I realized that the last line (and many more similar lines)
would probably need to be writen thusly:

        Key1 = Node[CTree.KEY]

I find this a little ugly, not to mention more typing.  I suppose
I could move these lines:

    LEAFSIZE = 4
    KEY,DATA,LEFT,RIGHT = range(LEAFSIZE)

out of the class and make them global, but this kind of runs
against the grain.

Is there another way to deal with "constants"?

 





More information about the Python-list mailing list