Define Constants

Discussion in 'Python' started by codecraig, Apr 20, 2005.

  1. codecraig

    codecraig Guest

    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.
    codecraig, Apr 20, 2005
    #1
    1. Advertising

  2. 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
    Steven Bethard, Apr 20, 2005
    #2
    1. Advertising

  3. 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
    Eric Nieuwland, Apr 20, 2005
    #3
  4. codecraig

    codecraig Guest

    Thanks for the input.

    i am renaming my module to be customthing. I noticed that is how many
    python modules are, so I will stick to the "convention".

    Thanks for the help.
    codecraig, Apr 21, 2005
    #4
  5. codecraig

    Guest

    , Apr 21, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Wash
    Replies:
    7
    Views:
    10,723
    Ashish M Bhonkiya
    Aug 30, 2004
  2. Ahmed Moustafa
    Replies:
    10
    Views:
    746
    Roedy Green
    Aug 17, 2003
  3. Allen
    Replies:
    13
    Views:
    558
  4. Ioannis Vranos

    #define constants

    Ioannis Vranos, Apr 3, 2008, in forum: C++
    Replies:
    7
    Views:
    398
    Ioannis Vranos
    Apr 4, 2008
  5. alex23
    Replies:
    7
    Views:
    223
    Jan Kaliszewski
    Aug 22, 2009
Loading...

Share This Page