Tag: Color AST object This exists SSI div tee Tin
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
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.
>>>classStudent: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#The age attribute does not existTraceback (most recent): File"<pyshell#20>", Line 1,inch<module>A.ageattributeerror:'Student'object has no attribute' Age'>>> SetAttr (A,' Age', 10)#Create an Age property after execution>>> A.age#There is an age attribute10>>> A.age = 12#equivalent to calling Object.name>>>A.age12
Python built-in function (--SETATTR)