In PHP we can refer to an attribute of an object dynamically like this $obj->{$field}
.
How can the same thing be achieved in python?
To get the value of an attribute dynamically, you can use getattr(obj, field_name)
.
To set the value dynamically, you can use setattr(obj, field_name, new_value)
.
To test whether the attribute exists, like isset
in PHP, you can use hasattr(obj, field_name)
.