Introduction:
In reading the source code, there are many forms of shorthand, one of the more commonly used is getattr() used to call a variable or method in a class, associated hasattr(), getattr(), setattr() function to learn the use of the same.
Main article:
1. hasattr(object, name)
Determines whether the name attribute exists in the object, and of course for python objects, the attribute contains variables and methods; if it does, it returns True, if it doesn't, it returns False; it should be noted that the name parameter is of type string, so regardless of whether it's a variable or a method to be determined, its name is passed as a string; the same is true for the getattr and the setattr. getattr and setattr;
>>> >>> class A(): name = 'python' def func(self): return 'A()Class methodsfunc()' >>> >>> hasattr(A, 'name') True >>> >>> hasattr(A, 'age') False >>> >>> hasattr(A, 'func') True >>>
2. getattr(object, name[, default])
Get the value of an object's attribute, if it exists then return the value of the attribute, if it doesn't exist there are two cases:
(1) If there is no default parameter, an error will be reported directly;
(2) Given the default parameter, if the object itself does not have a name attribute, the given default value is returned;
If the given attribute name is a method of the object, the return is a function object, you need to call the function object to get the return value of the function; call is the function object followed by parentheses, such as func for func();
Also of note:
If the given method func() is an instance function, it cannot be writtengetattr(A, 'func')(),
Because fun() is an instance function, it can't be called with an object of class A. It should be written asgetattr(A(), 'func')();
Difference between instance function and class function:
Instance functions are defined by def func(self): directly, and functions defined this way can only be called by instantiating the class with the instantiated object of the class;
In contrast, class function definitions need to be decorated with @classmethod, the default argument to the function is usually cls, and class functions can be called directly through the class object without instantiating the class;
>>> >>> class A(): name = 'python' def func(self): return 'Hello world' >>> >>> getattr(A, 'name') 'python' >>> >>> getattr(A, 'age')# Error if age variable does not exist Traceback (most recent call last): File "<pyshell#464>", line 1, in <module> getattr(A, 'age') AttributeError: class A has no attribute 'age' >>> >>> getattr(A, 'age', 20) >>> >>> getattr(A, 'func') <unbound method > >>> >>> getattr(A, 'func')()# func() function can't be called by an object of class A, so an error is reported, it needs to be called by an instantiated object of the class. Traceback (most recent call last): File "<pyshell#470>", line 1, in <module> getattr(A, 'func')() TypeError: unbound method func() must be called with A instance as first argument (got nothing instead) >>> >>> getattr(A(), 'func')() 'Hello world' >>> >>> class A(object): name = 'python' @classmethod def func(cls): return 'the method of A object.' >>> >>> getattr(A, 'func')() 'the method of A object.' >>>
3. setattr(object, name, value)
Assigns value to the name attribute of an object; if the given attribute name originally existed on the object, setattr changes the value of the attribute to the given value; if the attribute name did not originally exist on the object, setattr creates the attribute on the object and assigns the value to the given value;
>>> >>> class A(): name = 'python' def func(self): return 'Hello world' >>> >>> setattr(A, 'name', 'java') >>> getattr(A, 'name') 'java' >>> >>> setattr(A, 'age', 20) >>> getattr(A, 'age') >>>
Generally first determine whether an attribute exists in the object, and return if it does; if not, add the attribute to the object and assign a value:
>>> >>> class A(): name = 'python' def func(self): return 'Hello world' >>> >>> if hasattr(A, 'age'): print getattr(A, 'age') else: setattr(A, 'age', 20) >>> >>> getattr(A, 'age') >>>
Feel it:
summarize
The above is a small introduction to the use of python hasattr (), getattr (), setattr () function, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!