| | |
getattr function
Thread Solved |
•
•
Join Date: Aug 2005
Posts: 18
Reputation:
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?
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?
Hmm. Here's one way around it, using exec() instead of getattr():
Something more in line with what you were thinking of might be:
It's tricky, but it works!
Python Syntax (Toggle Plain Text)
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)
Python Syntax (Toggle Plain Text)
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)
Vi veri veniversum vivus vici
•
•
Join Date: Sep 2005
Posts: 1
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Avner .H.
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?
Python Syntax (Toggle Plain Text)
fn = getattr(__import__('example_module'), 'example_function')
![]() |
Similar Threads
- getattr function 2 (Python)
- Function that returns void (C++)
Other Threads in the Python Forum
- Previous Thread: Random Graphics (Python)
- Next Thread: Python's Debugger
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile shebang slicenotation split ssh string strings table terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wx.wizard wxpython xlwt






