943,796 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2217
  • Python RSS
Sep 26th, 2005
0

getattr function 2

Expand Post »
Hey everybody...

Does sombody knows how can i use the getattr function in order to call to a sub function defined inside another one?
somthing like this code:

# ----------------------------
def func1 (self, func_name) :
def func2 () :
print "Blaaaa"
def func3 () :
print "Yaaaaa"

getattr(?, func_name)()

self.func1("func3")
# ----------------------------

what should i put instead of the question mark (?) ?
i tried to write self.func1, but i doesn't work, because it looks
for a function attribute...
maybe there's another solution to this problem?

please help...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Avner .H. is offline Offline
18 posts
since Aug 2005
Sep 26th, 2005
0

Re: getattr function 2

Hi!

Are you sure this can be done with getattr? Well, I don't know if that's what you want, but this works:
Python Syntax (Toggle Plain Text)
  1. class A:
  2. def func1(self, func_name):
  3. def func2():
  4. print "Blabla"
  5. def func3():
  6. print "Yaaaaa"
  7. eval(func_name)
  8.  
  9. a = A()
  10. a.func1("func2()")
Could you explain what you want to do, maybe there's a better/simpler method

Regards, mawe
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Sep 26th, 2005
0

Re: getattr function 2

Do you mean something like this ...
Python Syntax (Toggle Plain Text)
  1. # a function can return a function
  2. def func1 () :
  3. def func2 () :
  4. print "Blaaaa"
  5. def func3 () :
  6. print "Yaaaaa"
  7.  
  8. return func2, func3
  9.  
  10.  
  11. func2, func3 = func1()
  12. func2()
  13. func3()
  14.  
  15. print func2 # test, what is it?
  16. print func3
Please put your code in code tags
[ code]your code here[/code]
Removed the space between [ and c for this to work!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

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: see my errors :-(
Next Thread in Python Forum Timeline: Help! Creating Acronyms!





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


Follow us on Twitter


© 2011 DaniWeb® LLC