Does Python have the equivalent of PHP's $this->{$var}
?
Something like self.{var name}
?
Does Python have the equivalent of PHP's $this->{$var}
?
Something like self.{var name}
?
To look up an attribute not known until runtime, use getattr
:
getattr(self, "varname")
From the docs:
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
To add an attribute to an object, the counterpart is setattr:
setattr(self, "varname", value)