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

3
Contributors
2
Replies
4
Views
12 Years
Discussion Span
Last Post by vegaseat
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

0

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 blocks

Edited by pyTony: fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.