943,623 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6670
  • Python RSS
Aug 10th, 2005
0

getattr function

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Avner .H. is offline Offline
18 posts
since Aug 2005
Aug 11th, 2005
0

Re: getattr function

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 11th, 2005
0

Re: getattr function

Hmm. Here's one way around it, using exec() instead of getattr():
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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!
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Aug 11th, 2005
0

Re: getattr function

Neat! Never thought of exec() that way!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 15th, 2005
0

Re: getattr function

Cool!
Thanks very much!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Avner .H. is offline Offline
18 posts
since Aug 2005
Sep 14th, 2005
0

Re: getattr function

Quote 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
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kerumai is offline Offline
1 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Random Graphics (Python)
Next Thread in Python Forum Timeline: Python's Debugger





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC