Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Python - quickest way to create class instance from string?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 Mike_Stoddart   Members   

182
Like
0Likes
Like

Posted 23 May 2008 - 06:07 AM

Say I have a python string containing a Python class definition. What is the easiest and quickest way to create an instance of that class, assuming you don't know that class is called but you do know it can derive from one of say four other abstract (so to speak) classes? Currently I'm just using compile and execing the string into a temporary module I create. I then search that module for a class that has one of the allowed base classes. I then eval the class' name to create an instance. Is there a quicker (performance wise) method for doing this? Thanks

#2 Kylotan   Moderators   

8821
Like
0Likes
Like

Posted 26 May 2008 - 11:06 PM

What about just calling eval on the original string?

#3 Vorpy   Members   

869
Like
0Likes
Like

Posted 27 May 2008 - 03:58 PM

Once you've found the class just use the () operator on it to construct it. Python classes act as constructor functions for themselves. Something like this:

x = searchForClass()
myInstance = x()

#4 Mike_Stoddart   Members   

182
Like
0Likes
Like

Posted 28 May 2008 - 01:24 AM

Quote:
Original post by Vorpy
Once you've found the class just use the () operator on it to construct it. Python classes act as constructor functions for themselves. Something like this:

x = searchForClass()
myInstance = x()



Vorpy: Is 'x' a string here, or the class object?

Kylotan: I'm not sure how eval would work differently from my code below.



# Compile the scriptlet.
code = compile(scriptlet, '<string>', 'exec')

# Create the new 'temp' module.
temp = imp.new_module("temp")
sys.modules["temp"] = temp

exec code in temp.__dict__

# Inspect the module.
classes = inspect.getmembers(temp, callable)
for name,value in classes:
if value.__module__ == "temp":
# Create the instance.
obj = eval("temp." + name)()

break

return obj



Thanks again.

#5 Kylotan   Moderators   

8821
Like
0Likes
Like

Posted 28 May 2008 - 01:59 AM

I guess I didn't entirely understand your first question, that's all.

#6 Vorpy   Members   

869
Like
0Likes
Like

Posted 28 May 2008 - 04:43 PM

In my example, "x" is the class object. In your code, I think it would be "value".

Instead of:

obj = eval("temp." + name)()

it would be:

obj = value()

Using eval is nearly always unnecessary.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.