472,792 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,792 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 1494
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.