getattr function 2

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

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 2

 
0
  #1
Sep 26th, 2005
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: getattr function 2

 
0
  #2
Sep 26th, 2005
Hi!

Are you sure this can be done with getattr? Well, I don't know if that's what you want, but this works:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
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: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: getattr function 2

 
0
  #3
Sep 26th, 2005
Do you mean something like this ...
  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!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 2014 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC