473,385 Members | 1,769 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,385 software developers and data experts.

Define Constants

Hi,
I have a question about how to define constants.

My directory structure looks like...

C:\
--> abc.py
--> utils
--> __init__.py
--> CustomThing.py

Ok, CustomThing looks like...

TOP = 0
LEFT = 1

class CustomThing:
def __init__(self):
self.foo = "foo"

so, from abc.py I have

from utils.CustomThing import CustomThing

print CustomThing.TOP

but i get an error: AttributeError: class 'CustomThing' has no
attribute 'TOP'
How can I access those??

Thanks.

Jul 19 '05 #1
2 5544
codecraig wrote:
Hi,
I have a question about how to define constants.

My directory structure looks like...

C:\
--> abc.py
--> utils
--> __init__.py
--> CustomThing.py

Ok, CustomThing looks like...

TOP = 0
LEFT = 1

class CustomThing:
def __init__(self):
self.foo = "foo"

so, from abc.py I have

from utils.CustomThing import CustomThing

print CustomThing.TOP

but i get an error: AttributeError: class 'CustomThing' has no
attribute 'TOP'

How can I access those??


Note that TOP and LEFT are delcared in the *module* CustomThing, not the
*class* CustomThing which is what you get if you do

from utils.CustomThing import CustomThing # the *class*

You should probably write your code as something like:

import utils.CustomThing
print utils.CustomThing.TOP

and if you need to use the class, write:

t = utils.CustomThing.CustomThing() # create a new CustomThing

Also, if you've just started this, it might be worth checking out
PEP8[1] which suggests that modules "should have short, lowercase names,
without underscores". If you're stuck with such a long module name, you
might try:

import utils.CustomThing as thing
print thing.TOP
t = thing.CustomThing()

STeVe

[1]http://www.python.org/peps/pep-0008.html
Jul 19 '05 #2
codecraig wrote:
My directory structure looks like...

C:\
--> abc.py
--> utils
--> __init__.py
--> CustomThing.py

Ok, CustomThing looks like...

TOP = 0
LEFT = 1

class CustomThing:
def __init__(self):
self.foo = "foo"

so, from abc.py I have

from utils.CustomThing import CustomThing

print CustomThing.TOP

but i get an error: AttributeError: class 'CustomThing' has no
attribute 'TOP'

How can I access those??


You're only importing the class. Try importing the whole module:

from utils import CustomThing
print CustomThing.TOP

--eric

Jul 19 '05 #3

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

Similar topics

8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by...
2
by: Andreas | last post by:
Hi! I'm using an IplImage library from camellia.sourceforge.net, and the testbench calls a file only containing code like the one below. In cam_morphomaths_code.c the real computation is made,...
7
by: Peng Yu | last post by:
I'm not very clear about when to use #define and when to use const? #define number 4 const int number = 4; If I just want to define a global constant, which way of the above is better? ...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
7
by: Don Wash | last post by:
Hi There! I'm trying to define constants so that I can refer those constants from any page of my ASP.NET website. I know I can use <appSettings> in web.config XML file but I don't want to parse...
7
by: Ioannis Vranos | last post by:
I think I read somewhere that #define constants in the style #define SOMETHING 6 are evaluated to long or unsigned long, depending on the situation and not int.
27
by: pereges | last post by:
I need to define the plancks constant 6.67 X 10 exp -34. Is it possible to do it using #define or would you suggest using a (static ?)const double.
7
by: Chris Saunders | last post by:
I'm not very familiar with C#. I have a static class and would like to define some constants similarily to Math.PI. Could someone show me how to go about doing this. So far no luck searching. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.