using exec() to instantiate a new object.
Patrick Mullen
saluk64007 at gmail.com
Fri Nov 7 19:14:19 EST 2008
On Fri, Nov 7, 2008 at 2:23 PM, RyanN <Ryan.Neve at gmail.com> wrote:
>
> 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 + ")")
>
Don't use exec. It's quite dangerous, and in your case is making
things much more complex than necessary. A much simpler way to do
what you want:
def addCountry(self,country_name):
self.countries.append(country(country_name))
There is no need to bind the result of "country(country_name)" to a name at all.
More information about the Python-list
mailing list