473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Instantiating an object when the type is only known at runtime

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
4 1511
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Scott Johnson | last post by:
I thought interfaces have no implementation so how can I create a DataReader from an IDataReader Interface and use it similarly to how I would use an OleDBDataReader? How is it that the block of...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.