English documents:
setattr
(object, name, value)
This is the counterpart of getattr()
. The arguments is an object, a string and an arbitrary value. The string may be name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, was setattr(x, ‘foobar‘, 123)
equivalent tox.foobar = 123
Set property values for an object
Description
1. The SetAttr function and the GetAttr function are corresponding. A property value that sets the object, one that gets the object property value.
2. The function has 3 parameters, and the function is to set the attribute value of the property named name to the Parameter object.
>>> class Student: def __init__ (self,name): self.name = name >>> a = Student (' Kim ') > >> a.name ' Kim ' >>> setattr (A, ' name ', ' Bob ') >>> a.name ' Bob '
3. The Name property can be an existing property of an object, updating its property value if it exists, and if the Name property does not exist, the object will create a property value for the name and store the value. Equivalent to calling Object.name = value.
>>> A.age # does not exist with The age attribute Traceback (most recent call last): File "<pyshell#20>", line 1, in <module>
a.ageattributeerror: ' Student ' object have no attribute ' age ' >>> setattr (A, ' age ', 10) # After execution create the Age property >>> ; A.age # exists with the age attribute 10>>> A.age = 12 # is equivalent to calling Object.name>>> A.age12
Python built-in function (--SETATTR)