x = getattr(self, source) will work just perfectly if source names ANY attribute of self, including the other_data in your example.

Answer from Alex Martelli on Stack Overflow
🌐
Finxter
blog.finxter.com › how-to-access-an-object-attribute-given-the-attribute-name-as-a-string
How to Access an Object Attribute Given the Attribute Name as a String? – Be on the Right Side of Change
You may know the following problem: You have an object’s attribute name as a string—say, you’ve read it from a file—and you want to access the attribute with the given name. But you cannot use the syntax object."attribute" because the dot notation only allows for name-access like this: object.attribute. How do you resolve this problem? This article will show you how! Quick Solution: There are four ways to access the object attribute in Python via the built-in functions:
Discussions

python - Access attributes of objects by string - Blender Stack Exchange
I'm trying to access object attributes by a variable. I have a bunch of strings, like "location", "rotation", "hide_render" that point to attributes of objects and would More on blender.stackexchange.com
🌐 blender.stackexchange.com
April 1, 2022
Accessing attributes of a class
How do I access the attributes of a class, such as, __bases__, __name__, __qualname__? When I do this, class C: pass dir(C) then it gives me, ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', ... More on discuss.python.org
🌐 discuss.python.org
0
0
February 22, 2022
how to access the class variable by string in Python? - Stack Overflow
But it doesn't work for a as ... can I accessa if I only have a string a? ... The reason self.__dict__ doesn't contain a key named a is that it's in self.__class__.__dict__. If you really want to do things manually, you can read up on the search order for attributes and check ... More on stackoverflow.com
🌐 stackoverflow.com
addressing class attribute with a variable?
I'm also not entirely sure what you're asking but you're probably looking for getattr(example, place). More on reddit.com
🌐 r/learnpython
17
7
February 14, 2025
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-getattr
Python getattr() | DigitalOcean
August 3, 2022 - The main reason to use python getattr() is that we can get the value by using the name of the attribute as String. So you can manually input the attribute name in your program from console. Again, if an attribute is not found you can set some default value, which enables us to complete some ...
🌐
Stack Exchange
blender.stackexchange.com › questions › 259292 › access-attributes-of-objects-by-string
python - Access attributes of objects by string - Blender Stack Exchange
April 1, 2022 - $\begingroup$ + it's really not safe to use eval on user input strings. It can execute malicious code easily. Try with attribute's name name, quit() and see what happens $\endgroup$ ... Find the answer to your question by asking.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-get-attribute-of-object-by-name
Access object attribute by String Name in Python | bobbyhadz
Use the getattr() function to access an object attribute by string. The getattr() function returns the value of the provided attribute of the object. ... Copied!class Employee(): def __init__(self, name, salary): self.name = name self.salary ...
🌐
GeeksforGeeks
geeksforgeeks.org › accessing-attributes-methods-python
Accessing Attributes and Methods in Python - GeeksforGeeks
March 29, 2025 - In Python, attributes and methods ... within a class. Attributes represent the properties or characteristics of an object, while methods define the actions or behaviors that an object can perform. Understanding how to access and manipulate both attributes and methods is important for effective object-oriented programming. Python provides built-in functions to dynamically interact with object attributes and methods. Let's explore them one by ...
🌐
Thedigitalcatonline
thedigitalcatonline.com › blog › 2015 › 01 › 12 › accessing-attributes-in-python
The Digital Cat - Accessing attributes in Python
January 12, 2015 - This usually happens when writing debuggers or inspection tools that let the user interactively specify the attributes they want to see. To perform this "indirect" access Python provides the getattr() builtin function, which accepts an object and the name of an attribute.
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › reference › datamodel.html
3. Data model — Python 3.14.3 documentation
This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. __slots__ reserves space for the declared variables and prevents the automatic creation of __dict__ and __weakref__ for each instance. ... When inheriting from a class without __slots__, the __dict__ and __weakref__ attribute of the instances will always be accessible.
🌐
Python.org
discuss.python.org › python help
Accessing attributes of a class - Python Help - Discussions on Python.org
February 22, 2022 - When I do this, class C: pass dir(C) then it gives me, ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', ...
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
For instance, if you have a function ... define a class with methods read() and readline() that get the data from a string buffer instead, and pass it as an argument. Instance method objects have attributes, too: m.__self__ is the instance object with the method m(), and m.__func__ is the function object corresponding to the method. By now you have ...
🌐
Snarky
snarky.ca › unravelling-attribute-access-in-python
Unravelling attribute access in Python
November 13, 2020 - The first method is __getattribute__() which is called when trying to access any and all attributes. The second is __getattr__() which is called when __getattribute__() raises an AttributeError.
🌐
Reintech
reintech.io › blog › python-getattr-method-tutorial
Python: Working with the getattr() Method | Reintech media
January 20, 2026 - The getattr() function is a powerful reflection tool in Python that enables dynamic attribute access on objects. Unlike direct dot notation (object.attribute), getattr() allows you to retrieve attributes using string-based names determined at runtime...
🌐
Reddit
reddit.com › r/learnpython › addressing class attribute with a variable?
r/learnpython on Reddit: addressing class attribute with a variable?
February 14, 2025 -

Is there a possibility to dynamically call class attributes based on variables?

example:

I have a class example, that has two attributes: first and second.

So you could define something like

test = example("foo", "bar") and you'd have test.first == "foo" and test.second == "bar".

Then I have another variable, say place, which is a string and is either place = "first" or place = "second".

Can I somehow call test.place?

There are a bazillion other uses for this, but at this current moment I'm trying to write a small "app" that has a few display strings, and I want to be able to select from two strings to display (two languages) based on command line argument.

🌐
Reddit
reddit.com › r/unity3d › how to get and set variable of a class instance using string (like getattr/setattr functions in python)
r/Unity3D on Reddit: How to get and set variable of a class instance using string (like getattr/setattr functions in Python)
December 6, 2023 -

Hi,

I am looking for a function in C#/Unity which allows me to get or set a variable of an instance using a string that is the same as the variable’s name.

I was using Python until recently, which does have such a function, so I want to demonstrate what I mean using that:

Let’s assume we have class called “Human”, and an instance of that class, “John”.
The class “Human” has a variable called “speed”, and “speed” variable of “John” is, for example, 10.

In Python, you can access the value ‘10’ by doing this:

getattr(John, "speed") #this would return the same value as John.speed, which is 10 

Also you could set that variable to another value, like:

setattr(John, "speed", 5) #after this, John.speed or getattr(John, "speed") would return 5. 

Is there a way to do this in C#/Unity?

I’m new to Unity, so although I tried to find the answer in the internet, I couldn’t find one. Can you help me?
Thanks.

🌐
Toptal
toptal.com › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: An Overly Thorough Guide | Toptal®
January 16, 2026 - You can manipulate the class attribute by accessing it through a particular instance and, in turn, end up manipulating the referenced object that all instances are accessing (as pointed out by Timothy Wiseman). Let’s go back to the Service I defined earlier and see how my use of a class variable could have led to problems down the road: {:lang='python'} class Service(object): data = [] def __init__(self, other_data): self.other_data = other_data ...
🌐
Python.org
discuss.python.org › python help
Setting an attibute of an object as a String - Python Help - Discussions on Python.org
July 7, 2022 - Hello everyone, I hope you’re doing well ! I am actually looking for a certain thing to do. I have a certain object “uut.adder” that might have a hundred different attributes (example: uut.adder.c1,uut.adder.b1, uut.adder.a3 and so on) so I would like to loop over ALL THESE so I’m trying ...
🌐
Andrea Minini
andreaminini.net › computer-science › python › accessing-object-attributes-in-a-python-class-using-format
Accessing Object Attributes in a Python Class Using Format - Andrea Minini
In lines 4 and 5, I create two instances of the Capital class, named Italy and France. Each instance is assigned the name of the respective capital city. To retrieve the value of the city attribute for the Italy object, you can write: print("The capital of Italy is {Capital.city}".format(Capital=Italy)) Within the string, the placeholder refers to the attribute Capital.city. The format() function tells Python which object to use (Italy in this case).
🌐
Turing
turing.com › kb › introduction-to-python-class-attributes
A Guide to Python Class Attributes and Class Methods
They can be accessed using the class name and through an instance of the class. Class attributes are defined outside of any method, including the init method, and are typically assigned a value directly.