0
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?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by redyugi
0

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)
0

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

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.