class Test:
    def __init__(self,  number):
        self.number = number
    def getnumber(self):
        return self.number

x = Test(54)
y = 'number'

print x.getnumber()
try:
    print(eval('%s.%s'% (x, y)))
except Exception,  e:
    print(e)

This isn't the code I want to fix, but it has the same error. I am trying to return a class function on runtime, but it does not work. What am I doing wrong?

Do you mean, that you want to do this:

class Test:
    def __init__(self,  number):
        self.number = number
    def getnumber(self):
        return self.number

x = Test(54)
y = 'number'

print x.getnumber()
print getattr(x,y)

Actually I got it on my own. But yeah, that is what I wanted. Thanks

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.