-1
class myclass:
  ab="python"
  bc="pyspark"
  de="sql"
  ef="azure" 
case 1:
print(myclass.ab)  #output: python
case 2:
x="ab"
myclass.x          #error: type object 'myclass' has no attribute 'x'

How do I access class variable 'ab' with the help of another variable 'x'?

ruohola
  • 21,987
  • 6
  • 62
  • 97
Vineeth Reddy
  • 198
  • 1
  • 8

1 Answers1

0

You can use the built-in getattr function:

>>> x = "ab"
>>> getattr(myclass, x)
'python'
ruohola
  • 21,987
  • 6
  • 62
  • 97