| | |
getattr function 2
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2005
Posts: 18
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Hi!
Are you sure this can be done with getattr? Well, I don't know if that's what you want, but this works:
Could you explain what you want to do, maybe there's a better/simpler method 
Regards, mawe
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)
class A: def func1(self, func_name): def func2(): print "Blabla" def func3(): print "Yaaaaa" eval(func_name) a = A() a.func1("func2()")

Regards, mawe
Do you mean something like this ...
Please put your code in code tags
[ code]your code here[/code]
Removed the space between [ and c for this to work!
Python Syntax (Toggle Plain Text)
# a function can return a function def func1 () : def func2 () : print "Blaaaa" def func3 () : print "Yaaaaa" return func2, func3 func2, func3 = func1() func2() func3() print func2 # test, what is it? print func3
[ code]your code here[/code]
Removed the space between [ and c for this to work!
May 'the Google' be with you!
![]() |
Similar Threads
- getattr function (Python)
- Function that returns void (C++)
Other Threads in the Python Forum
- Previous Thread: see my errors :-(
- Next Thread: Help! Creating Acronyms!
Views: 2014 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache approximation argv array beginner binary book calculator change cipher clear code compile converter countpasswordentry cturtle dictionaries dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java keyboard library line linux list lists loop maze mouse mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive remote script scrolledtext search session signal singleton socket ssh string strings strip table terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable verify vigenere windows wordgame wxpython xlwt






