Sign in to follow this  
Tac-Tics

Python -- creating a object dynamically from a string

Recommended Posts

Hi. I'm using python for a stratego-like game. I've run into a problem which I am sure Python can take care of very easily. I want to take a list of strings and turn them into unit objects. I have a base class called Unit. There are several subclasses such as Marshall, Bomb, Flag, Spy, General, Scout, etc. I also have a list of strings corresponding to those names. Each name appears once for every copy of that unit you start with. So, it looks like:
["General", "Major", "Major", "Major", ... ]
Since you start off with 1 general and 3 major units. What I want to do is run through this list and somehow generate the correct type of unit from a string. I would think it would follow the form:
for unit in unitNames:
   unitClass = ????(unit)
   unitList.append(apply(unitClass), ...)
Does Python have a function or way to fill in the ???? (a function to convert a string to the class/type that corresponds to that type). Thanks in advance.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can use the python module 'new'. There's a method called 'instance' which takes a class object and returns you a new instance of that class. Since this method takes a class object and not a string you'll also need a dictionary to look up the string and retrieve the appropriate class object.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why not use a list of classes?
[General, Major, Major, Major, ... ]

Classes are objects in Python.

Also, you can do eval("General") to get the General-class. Just be aware that you can feed anything to eval, e.g. eval("formatHardDrive()") so make sure your users won't get to pass in stuff there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As the names of classes are really treated as functions (for all intents and purposes), there's really no good reason why you shouldn't be able to do something like:


obj = eval(classList[name] + "()")


or somethingn of the like.

Share this post


Link to post
Share on other sites
Thank you all for your answers

>>Why not use a list of classes?
>>[General, Major, Major, Major, ... ]

I didn't think of that although, unlike the other ways people answered, that is the one way I *should* have known how to do it.

Still, keeping them strings making it easy if I for some reason want to write this as a *.ini file.

Thanks again all.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Tac-Tics
Thank you all for your answers

>>Why not use a list of classes?
>>[General, Major, Major, Major, ... ]

I didn't think of that although, unlike the other ways people answered, that is the one way I *should* have known how to do it.

Still, keeping them strings making it easy if I for some reason want to write this as a *.ini file.

Thanks again all.
I think you'd be better off having them as class-objects in the array that you use at runtime, and only converting to/from strings IF you have to write an ini file. You can convert a class object to a string like this: General.__name__. And other direction works with eval.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
obj = eval(classList[name] + "()")
I think eval(classList[name])() is clearer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this