What is "dynamic assignment of an attribute" in python?

135 Views Asked by At

I've used Python sparingly, so maybe I'm just not privy to some verbiage. Someone at a talk today said that they want to avoid "dynamic assignment of an attribute" and didn't provide an example.

I know what dynamically creating an attribute of a class means, but what does dynamic assignment mean? This seems to imply that there's static assignment in python, but python is a dynamically typed language.

1

There are 1 best solutions below

2
juanpa.arrivillaga On

Typically, this means that the attribute itself is created dynamically as a string then assigned to an instance:

class Foo:
...     pass
...
>>> foo = Foo()
>>> attr = "some_dynamically_created_string"
>>> setattr(foo, attr, 42)
>>> foo.some_dynamically_created_string
42