using exec() to instantiate a new object.

RyanN Ryan.Neve at gmail.com
Fri Nov 7 17:23:45 EST 2008


Hello,

I'm trying to teach myself OOP to do a data project involving
hierarchical data structures.

I've come up with an analogy for testing involving objects for
continents, countries, and states where each object contains some
attributes one of which is a list of objects. E.g. a country will
contain an attribute population and another countries which is a list
of country objects. Anyways, here is what I came up with at first:

class continent(object):
    def __init__(self,continent_name):
        self.name = continent_name
        self.countries = []
    def addCountry(self,country_name):
        self.countries.append(country_name)
    def listCountries(self):
        for country in self.countries:
            print country.name, "pop:",country.population,", states:"
            country.listStates()

class country(object):
    def __init__(self,name):
        self.name = name
        self.population = 0
        self.states = []
    def addState(self,state_name):
        self.states.append(state_name)

    def listStates(self):
        for state in self.states:
            print " ",state.name,"pop:",state.population
            state.stateInfo()

class state(object):
    def __init__(self,state_name):
        self.name = state_name
        self.color = 'unknown'
        self.counties = []
        self.population = 0
    def addCounty(self,county):
        self.counties.append(county)
    def stateInfo(self):
        print "       color:",self.color
        print "       counties",self.counties[:]


NAm = continent('NAm')
usa= country('usa')
canada = country('canada')
mexico = country('mexico')
florida = state('florida')
maine = state('maine')
california = state('california')
quebec = state('quebec')

NAm.addCountry(usa)
NAm.addCountry(canada)
NAm.addCountry(mexico)
usa.addState(maine)
usa.addState(california)
usa.addState(florida)
canada.addState(quebec)
florida.addCounty('dade')
florida.addCounty('broward')
maine.addCounty('hancock')
california.addCounty('marin')

florida.population = 1000
california.population = 2000
maine.population = 500
quebec.population = 1000
florida.color = maine.color = california.color = 'blue'
NAm.listCountries()

--------------------------------------------------------------------------
so this works but is far more cumbersome than it should be.
I would like to create an object when I add it

so I wouldn't have to do:
usa= country('usa')
NAm.addCountry(usa)

I could just do
NAm.addCountry('usa')

which would first create a country object then add it to a countries
list

to do this I tried:

    def addCountry(self,country_name):
        # create an instance of country
        exec(country_name + "= country('" + country_name + "')")
        # Add this new instance of a country to a list
        exec("self.countries.append(" + country_name + ")")

Which doesn't give an error, but doesn't seem to create an instance of
the country object.

Does this make sense? Can this be done?
For my real project, I won't know the names and quantities of objects.
They will be highly variable and based on data contained in the
"parent" object.

Thanks



More information about the Python-list mailing list