How to access a class attribute using another variable?

Vineeth Reddy
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

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to access a variable from another class using Qt C++?

how to access variable from another class in python

How to access variable from another class directly(i.e. without using class name or object)?

Using a variable as an attribute of a class

How to access an attribute of an object defined inside another class in Python?

How may I access an attribute of a class from the __init__ of another?

How to reference an attribute from one class using another class

How do I access a variable in a class that was defined before in another class?

Access class level attribute of another class?

Using another class variable

Dart: How to access a class attribute of an object inside another class? (Nested Class)

How to access an attribute of an object using a variable in django template?

How to access the attribute name when using a class as an enum?

How can I access the changed variable from another class

How to access variable from outside class in another file

How to access private static variable in static member function of another class?

How do I correctly access a variable from a Class in another file?

How to access variable from one class in another? Swift Code

How can I access a variable in another function in a class?

How to access a variable from another function in the same class in Angular?

How can i Access a private variable in another class in C#

How would I access a published variable from another class?

How to Access an Object's Variables/Methods using Another Variable

Java how to access methods using an instance from another class

How to access a method from another class without using extends keyword?

How to access a view inside of another class using Kotlins view binding

How can I create an interface attribute in a class in Unity using C# and access the methods of the interface from this attribute?

How to change variable from one class using another in C#

Access swift structure attribute using a variable