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 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 access (get or set) object attribute given string corresponding to name of that attribute
- 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
- Convert nested Python dict to object?
- How can I create a copy of an object in Python?
- Referring to the null object in Python
- Getting an instance name inside class __init__() [duplicate]
- What is the difference between __init__ and __call__?
- Python string to attribute
- How do I save and restore multiple variables in python?
- Set attributes from dictionary in python
- Difference between methods and attributes in python
- How do I correctly setup and teardown for my pytest class with tests?
- Why is the cmp parameter removed from sort/sorted in Python3.0?
- Why does object.__new__ work differently in these three cases
- AttributeError: partially initialized module ‘turtle’ has no attribute ‘Turtle’ (most likely due to a circular import)
- Null object in Python
- How to assign a new class attribute via __dict__?
- “Enabling” comparison for classes [duplicate]
- The inheritance of attributes using __init__
- Python double underscore mangling
- Get the object with the max attribute’s value in a list of objects