473,398 Members | 2,335 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,398 software developers and data experts.

Constants in Python

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"?


Jul 18 '05 #1
2 1671
"Brent W. Hughes" <br**********@comcast.net> wrote in message
news:Slb%c.389920$%_6.21712@attbi_s01...
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]


How about this?

Key1 = Node[self.KEY]

Seems consistent with other class-relative usages.
Jul 18 '05 #2
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 (jo**@grulic.org.ar) -- Random fortune:
Deliver yesterday, code today, think tomorrow.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBPbPIgPqu395ykGsRAsCiAKCRJzA0qPvfPW/OBXvP4BwWp7Z+TQCgwoDa
Kw2lBVDedqSN2OpIK+p4jXU=
=w4DA
-----END PGP SIGNATURE-----

Jul 18 '05 #3

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

Similar topics

2
by: Sriram Chadalavada | last post by:
Hello everyone, I am a newbie to Python with experience in C programming. For my project, I am re-writing C routines as Python functions. I am currently using raw numerical values and was...
9
by: Chris Gonnerman | last post by:
A while back (a long while actually) I was a participant in a long argument over decimal and rational numbers, and their inclusion into the core of Python. Last night in a dream I came up with...
0
by: Tim Roberts | last post by:
I've done a lot of COM work from Python, including control of the Office applications. However, I have a question that I've never been able to answer. Is there a way to programmatically fetch...
8
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
8
by: Grant Edwards | last post by:
How do I get rid of the following warning? <path>.py:233: FutureWarning: hex/oct constants > sys.maxint will return positive values in Python 2.4 and up...
4
by: Tomasz Lisowski | last post by:
Hi, We are distributing our Python application as the short main script (.py file) and a set of modules compiled to the .pyc files. So far, we have always treated .pyc files as portable between...
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
1
by: Nathan Harmston | last post by:
Hi All, I ve got a single module which I m using to contain a lot of dictionaries, constants, general information, which are used by various other modules. However I can't seem to access them: ...
0
by: Barry Newberger | last post by:
I am working on a Outlook COM project. For some reason win32com.client.constants quit working between runs of one of my test scripts. It's supposed to acquire attributes for all constants...
1
by: Noah | last post by:
I'm trying to match against Event.type for KeyPress and ButtonPress. Currently I'm using integer constants (2 and 4). Are these constants defined anywhere? The docs talk about KeyPress and...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.