By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Instantiating an object when the type is only known at runtime

P: n/a
Hi,

I am trying to replace the eval() in the following code:

def myfunc(type, table):
module = __import__(type)
type = 'module' + '.' + type
obj = eval(type)
return obj(row[table.c.name], row[table.c.handle])

I am out of ideas. Any hints?

Thanks,
-Samuel

Oct 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Samuel wrote in news:11**********************@c28g2000cwb.googlegr oups.com
in comp.lang.python:
Hi,

I am trying to replace the eval() in the following code:

def myfunc(type, table):
module = __import__(type)
type = 'module' + '.' + type
obj = eval(type)
return obj(row[table.c.name], row[table.c.handle])
>>m = __import__( "StringIO" )
x = getattr( m, "StringIO" )()
x
<StringIO.StringIO instance at 0x00A47710>
>>>
Rob.
Oct 3 '06 #2

P: n/a

"Samuel" <kn******@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
Hi,

I am trying to replace the eval() in the following code:

def myfunc(type, table):
module = __import__(type)
type = 'module' + '.' + type
obj = eval(type)
return obj(row[table.c.name], row[table.c.handle])

I am out of ideas. Any hints?
Perhaps what you need is a dict 'types' mapping strings to types/classes.
Then the last two lines might become

return types[type](row[table.c.name], row[table.c.handle])

The trick of mapping names to callables for runtime choice of what to call
has several uses.

Terry Jan Reedy

Oct 3 '06 #3

P: n/a
Thanks, that's what I was looking for.
>m = __import__( "StringIO" )
x = getattr( m, "StringIO" )()
x
<StringIO.StringIO instance at 0x00A47710>
>>
For the records: If the module is already loaded, this also works:

if my_type_is_not_yet_loaded:
module = __import__(type)
obj = getattr(module, type)
else:
obj = globals().get(type)
resource = obj(my_arg1, my_arg2)

Thanks again,
-Samuel

Oct 3 '06 #4

P: n/a
On Tue, 2006-10-03 at 18:19, Samuel wrote:
Thanks, that's what I was looking for.
>>m = __import__( "StringIO" )
>>x = getattr( m, "StringIO" )()
>>x
<StringIO.StringIO instance at 0x00A47710>
>>>

For the records: If the module is already loaded, this also works:

if my_type_is_not_yet_loaded:
module = __import__(type)
obj = getattr(module, type)
else:
obj = globals().get(type)
resource = obj(my_arg1, my_arg2)
You seem to be under the impression that importing an already imported
module is a hideously expensive operation that must be avoided at all
costs. It's not. If you import an already imported module, python simply
returns the module object from sys.modules without executing any of the
module's code.

Your code will be much easier to read if you eliminate the
"my_type_is_not_yet_loaded" check, whatever that may be under the hood,
and always perform the __import__. It's also likely to be at least as
fast, but I couldn't run a timing comparison even if I wanted to because
you didn't post working code.

-Carsten
Oct 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.