| | |
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: 2013 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
anti assignment avogadro beginner bluetooth code convert count csv decimals def dictionary dynamic dynamically enter examples excel file float format frange ftp function gnu gui heads homework import input jaunty java lapse leftmouse line lines linux list lists loop microcontroller module mouse multiple newb number numbers output parsing path pointer port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random raw_input recursion recursive redirect scrolledtext slicenotation software sqlite ssh stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape windows word wx.wizard wxpython






