[Tutor] calling class instances in functions
kent37 at tds.net
Wed Sep 12 13:33:20 CEST 2007
Ara Kooser wrote:
Your question is not very clear but I made some guesses below.
> zz = raw_iput("What kind of yeast cell do you want to add?
> #Here I want to call this instace of GoodYeast from yeast_cell.py
So you want the user to input the name of a class and you create an
instance of the class? Try
cls = getattr(yeast_cell, zz)
instance = cls()
> class GoodYeast:
> #Cooperates all the time
> YEAST = 'G'
> #Should change generic variable @ in the world to G???
This is just assigning a class variable. The assignment is made at the
time the class is defined, not when an instance is created, so that is
not what you want either.
> def __init__(self,name):
> self.name = name
> currency = 0
> location = 
I agree with John that changing a global variable in another module when
a class instance is created doesn't seem like a great design, but you
can do it here with
import main # or whatever the main module is called
main.YEAST = 'G'
In general it is good to avoid circular dependencies like this, they
lead to a variety of complications. Maybe YEAST could be a variable in
the yeast_cell module?
More information about the Tutor