954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getattr function

hello fellows,

I seem to have a little problem:

I tried to use the function getattr(object, attribute) with two strings which is being read from a xml file as arguments, and seem to have a problem with sending the first one as a string. the function thinks that the string object itself is the object argument, when what i meant was that the VALUE of my string will be sent to the function as a name of a module in my program.

the second argument however, isn't doing any problems, and is being treated as the object of the value of the string, and not as a string object itself.
weird!

maybe a conversion between a string object and a module object is possible? i don't know...

help?

Avner .H.
Newbie Poster
18 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Would be nice to have an actual example!

Check the "mimic a C struct type with an empty Python class " in the Starting Python sticky. You could do something along that line.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Hmm. Here's one way around it, using exec() instead of getattr():

import sys
# For our example, let's use random.sample() as the desired attribute
# This function takes as input a list and an integer n and returns a
# random, non-redundant selection of n elements from the list
modname = "random"
attname = "sample"
# Import the module using exec, set the function to a wrapper variable
exec("import "+modname)
exec("sampleWrapper = "+modname+"."+attname)
# Test the wrapper variable
print sampleWrapper([1, 2, 3, 4, 5, 6], 2)

Something more in line with what you were thinking of might be:

exec("import "+modname)
exec("mod = "+modname)
# Now we can use mod as though it were the actual module
sampleWrapper = getattr(mod, "sample")
print sampleWrapper([1, 2, 3, 4, 5, 6], 2)

It's tricky, but it works!

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

Neat! Never thought of exec() that way!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Cool!
Thanks very much!

Avner .H.
Newbie Poster
18 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

hello fellows,

I seem to have a little problem:

I tried to use the function getattr(object, attribute) with two strings which is being read from a xml file as arguments, and seem to have a problem with sending the first one as a string. the function thinks that the string object itself is the object argument, when what i meant was that the VALUE of my string will be sent to the function as a name of a module in my program.

the second argument however, isn't doing any problems, and is being treated as the object of the value of the string, and not as a string object itself. weird!

maybe a conversion between a string object and a module object is possible? i don't know...

help?

Try

fn = getattr(__import__('example_module'), 'example_function')

The getattr builtin requires the moduleobject, therefore you have to load that module using __import__() first.

kerumai
Newbie Poster
1 post since Sep 2005
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You