problem with defining a global class instance

S

sharath B N

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
 
W

wittempj

sharath said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top