Reputation: 136
I tried in all ways, could not get any solution for it.
I'm stuck in an application, i will give similar example ,
i have some strings,
arg = "school"
arg_2 = "college"
school = "enjoy"
college = "study"
i want to use it in code as below
if ( arg == arg )
\\ want to print content of school here,
else
\\ want to print content of college here,
can i make it with the help of string 'arg' only? i don't want to use the name of string 'school' here. is there any method to do that?
Upvotes: 0
Views: 113
Reputation: 113994
I am assuming that your goal is to get indirect access to values. In that case, consider putting your variables in a class or a dictionary. Here is an example of the class approach:
class mydata(object):
arg = "school"
arg_2 = "college"
school = "enjoy"
college = "study"
def value_of_value(self, s):
return getattr(self, getattr(self, s))
x = mydata()
print 'arg->', x.value_of_value('arg')
print 'arg_2->', x.value_of_value('arg_2')
This produces:
arg-> enjoy
arg_2-> study
Upvotes: 0
Reputation: 77093
You can use locals
to do this
>>> arg = "school"
>>> arg_2 = "college"
>>> school = "enjoy"
>>> college = "study"
>>> locals()[arg]
'enjoy'
>>> locals()[arg_2]
'study'
So you could simply print a statement like
>>> "{} at {}".format(locals()[arg], arg)
'enjoy at school'
>>> "{} at {}".format(locals()[arg_2], arg_2)
'study at college'
PS: doing arg == arg
is completely redundant, it will always evaluate to True
Upvotes: 2