954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getattr function 2

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...

Avner .H.
Newbie Poster
18 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Hi!

Are you sure this can be done with getattr? Well, I don't know if that's what you want, but this works:

class A:
    def func1(self, func_name):
        def func2():
            print "Blabla"
        def func3():
            print "Yaaaaa"
        eval(func_name)

a = A()
a.func1("func2()")

Could you explain what you want to do, maybe there's a better/simpler method :)

Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Do you mean something like this ...

# 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

Please put your code in code tags
[ code]your code here[/code]
Removed the space between [ and c for this to work!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You