setattr Function Instance Usage of python
- 2021-08-21 21:02:50
- OfStack
Now this site has been learning language programs for a long time. But after understanding, if let this site to learn the language to start with the entrance, 1 is to begin to understand from the mastery of functions, the reason is very simple, any 1 code string is composed of functions, which is just like when we play games, first master the "w", "Q" key 1, so as to flexibly use the good operation of the whole program. I also bring you a good function-setattr.
Step 1, Describe
The setattr function corresponds to the getatt () function and is used to set the value of the property, which must exist.
Step 2, setattr syntax
setattr(object, name, value)
Step 3, Parameters
object--Object.
name--String, object property.
value--Attribute value.
Step 4, return value
Step 5, the following example shows how to use setattr:
>>>class A(object): ... bar = 1 ...
>>> a = A()
>>> getattr(a, 'bar') # Get properties bar Value 1
>>> setattr(a, 'bar', 5) # Setting Properties bar Value
>>> a.bar 5
setattr function instance extension
Change the value of the "age" property of the "Person" object
class Person:
name = "John"
age = 36
country = "Norway"
setattr(Person, 'age', 40)
Syntax
setattr(object, attribute, value)