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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.