Constants in Python

John Lenton john at grulic.org.ar
Tue Sep 7 15:12:40 CEST 2004


On Tue, Sep 07, 2004 at 05:06:58AM +0000, Brent W. Hughes wrote:
> 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"?

not as far as I know. I'm just posting to underline what Andrew just
said: you want to do self.KEY, not CTree.KEY; the latter is a disaster
from a code reusability standpoint, because you just rendered your
class unsubclassable. Well, not quite, but you certainly restricted it
a lot.

-- 
John Lenton (john at grulic.org.ar) -- Random fortune:
Deliver yesterday, code today, think tomorrow.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20040907/d8fcc913/attachment.pgp>


More information about the Python-list mailing list