0

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?

zerecees
  • 627
  • 4
  • 13
Lahiru
  • 1
  • 2

1 Answers1

0

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).

kaya3
  • 41,043
  • 4
  • 50
  • 79