problem with defining a global class instance

Discussion in 'Python' started by sharath B N, Nov 17, 2006.

  1. sharath B N

    sharath B N Guest

    hi,
    i am sort of newbie to python. I am trying to do a super Market
    simulation with OOP in python. I have problems with using a class
    instance as global...
    def generate (... ,....,...)

    " in this function i define the global variables "
    global stock,stockManager, manager etc.


    class Manager
    ....
    ....
    ....
    def create_stockManager(..)
    """ this is a method in class manager"""
    stockManager = StockManager( name)
    stockManager.create_Stock(..)


    now this gives an attribute error sayin .... stockManager has no
    attribute create_Stock

    if i create the StockManager instance in the generate func
    itself...then this problem doesnt come....but i need it this way for
    the program to make sense..
    can somebody help me
    thnks
    Sharath
     
    sharath B N, Nov 17, 2006
    #1
    1. Advertising

  2. sharath B N

    Guest

    sharath B N wrote:
    > hi,
    > i am sort of newbie to python. I am trying to do a super Market
    > simulation with OOP in python. I have problems with using a class
    > instance as global...
    > def generate (... ,....,...)
    >
    > " in this function i define the global variables "
    > global stock,stockManager, manager etc.
    >
    >
    > class Manager
    > ...
    > ...
    > ...
    > def create_stockManager(..)
    > """ this is a method in class manager"""
    > stockManager = StockManager( name)
    > stockManager.create_Stock(..)
    >
    >
    > now this gives an attribute error sayin .... stockManager has no
    > attribute create_Stock
    >
    > if i create the StockManager instance in the generate func
    > itself...then this problem doesnt come....but i need it this way for
    > the program to make sense..
    > can somebody help me
    > thnks
    > Sharath


    Python uses the concept of namespaces, the keyword 'global' binds a
    local variable to the global namespace, take as example:

    # this amount is in the global namespace
    amount = 98

    def get_amount_local():
    # this amount is local in this function
    amount = 23
    return amount

    def get_amount_global():
    # this global is taken from the gloabl namespace
    global amount
    return amount

    print get_amount_local() # so this will print 23
    print get_amount_global() # so this will print 98

    see http://docs.python.org/ref/assignment.html
     
    , Nov 17, 2006
    #2
    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. johny smith
    Replies:
    8
    Views:
    421
    Peter Koch Larsen
    Jul 2, 2004
  2. Sridhar R
    Replies:
    14
    Views:
    1,410
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. OK
    Replies:
    5
    Views:
    624
    Peter Bromberg [C# MVP]
    Nov 26, 2007
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
  5. Mikkel Kroman
    Replies:
    4
    Views:
    105
    Mikkel Kroman
    Feb 8, 2010
Loading...

Share This Page