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'?
You can use the built-in getattr
function:
>>> x = "ab"
>>> getattr(myclass, x)
'python'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments