There are built-in functions called getattr
and setattr
getattr(object, attrname)
setattr(object, attrname, value)
In this case
x = getattr(t, 'attr1')
setattr(t, 'attr1', 21)
More Related Contents:
- How to access object attribute given string corresponding to name of that attribute
- How do you programmatically set an attribute?
- Python dictionary from an object’s fields
- How to check if an object has an attribute in Python?
- How can I create an object and add attributes to it?
- How to check if an object has an attribute?
- How do I check if an object has an attribute?
- Python object attributes – methodology for access
- What’s the difference between the square bracket and dot notations in Python?
- Calling a function of a module by using its name (a string)
- How to know if an object has an attribute in Python
- What is the difference between class and instance attributes?
- How to print instances of a class using print()?
- Extracting an attribute value with beautifulsoup
- Convert nested Python dict to object?
- Is everything an object in Python like Ruby?
- Iterate over object attributes in python [duplicate]
- Python string to attribute
- Get the name of a pandas DataFrame
- python class attribute
- Why is `object` an instance of `type` and `type` an instance of `object`?
- Why do I get a TypeError that says “takes no arguments (1 given)”? [duplicate]
- Why is the cmp parameter removed from sort/sorted in Python3.0?
- Python: Make class iterable
- How do I determine if an object has an attribute in Python?
- Null object in Python
- Why is `self` in Python objects immutable?
- How to assign a new class attribute via __dict__?
- “Enabling” comparison for classes [duplicate]
- Openpyxl check for empty cell