getattr function

Thread Solved

Join Date: Aug 2005
Posts: 18
Reputation: Avner .H. is an unknown quantity at this point 
Solved Threads: 0
Avner .H. Avner .H. is offline Offline
Newbie Poster

getattr function

 
0
  #1
Aug 10th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: getattr function

 
0
  #2
Aug 11th, 2005
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: getattr function

 
0
  #3
Aug 11th, 2005
Hmm. Here's one way around it, using exec() instead of getattr():
  1. import sys
  2. # For our example, let's use random.sample() as the desired attribute
  3. # This function takes as input a list and an integer n and returns a
  4. # random, non-redundant selection of n elements from the list
  5. modname = "random"
  6. attname = "sample"
  7. # Import the module using exec, set the function to a wrapper variable
  8. exec("import "+modname)
  9. exec("sampleWrapper = "+modname+"."+attname)
  10. # Test the wrapper variable
  11. print sampleWrapper([1, 2, 3, 4, 5, 6], 2)
Something more in line with what you were thinking of might be:
  1. exec("import "+modname)
  2. exec("mod = "+modname)
  3. # Now we can use mod as though it were the actual module
  4. sampleWrapper = getattr(mod, "sample")
  5. print sampleWrapper([1, 2, 3, 4, 5, 6], 2)
It's tricky, but it works!
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: getattr function

 
0
  #4
Aug 11th, 2005
Neat! Never thought of exec() that way!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 18
Reputation: Avner .H. is an unknown quantity at this point 
Solved Threads: 0
Avner .H. Avner .H. is offline Offline
Newbie Poster

Re: getattr function

 
0
  #5
Aug 15th, 2005
Cool!
Thanks very much!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1
Reputation: kerumai is an unknown quantity at this point 
Solved Threads: 1
kerumai kerumai is offline Offline
Newbie Poster

Re: getattr function

 
0
  #6
Sep 14th, 2005
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?
Try
  1. fn = getattr(__import__('example_module'), 'example_function')
The getattr builtin requires the module object, therefore you have to load that module using __import__() first.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC