[Tutor] calling class instances in functions

John Fouhy john at fouhy.net
Wed Sep 12 06:03:23 CEST 2007


I'm not sure I understnad your quesiton..

You can put classes into lists, dictionaries, et cetera.  For example:

##
class Foo(object):
    pass

class Bar(object):
    pass

class Baz(object):
    pass

my_classes = { 'foo':Foo, 'bar':Bar, 'baz':Baz }

thing = my_classes['bar']()
##

It seems quite strange to me, though, to have the instantiation of a
class change a global variable.  Maybe you could do something like
this:

##
class GoodYeast(object):
    pass # etc

class BadYeast(object):
    pass # etc

class World(object):
    yeasts = { 'good':GoodYeast, 'bad':'BadYeast }
    def init(self):
        self.yeast = None

    def setYeast(self, yeast):
        self.yeast = self.yeasts[yeast]()

w = World()
w.setYeast('good')
##

But I confess that I have not read or run your code, so I'm not sure
if this is the best way forward for you..

-- 
John.


More information about the Tutor mailing list