I'm using Python 2.7.
Take, for example, the following code:
class Apple:
def __init__(self, n):
self.n = n
def printVariable(self, s): # print variable named s
if hasattr(self, s):
print ...
What would I replace ...
with to print self.'s'
. For example, if I called printVariable('n')
, what would I put in place of ...
to print n
?
Of course, self.s
would not work because first of all there is no attribute self.s
, but more importantly, that's printing a different variable, not the variable self.'s'
I want to print the variable whose name is represented by the string s
that is passed to the method.
I'm sorry about the inherently confusing nature of self.s
and self.'s'
and s
in this question.