Python object-oriented (1), python object-oriented
I. dir built-in functions II. Class 1. Definition
Big camper naming method
2. Create
Object variable = Class Name ()
3. self parameter (which object calls method, and reference of which object is self is similar to this)
(1) Add attributes
# In the external code of the class, you can directly set an attribute through. To do this, tom. name = "Tom"
(2) Call attributes
- Self. Access Object Attributes
- Self. Call other object Methods
(PS is not recommended for external attribute assignment)
(3) initialization method
When an object is created using the class name, the initialization method _ init () _ is automatically called ()__
Example:
(4) built-in Methods
- _ Del _ Method
- Del object (an object can be deleted) If del is not active, the object performs del after all code is executed.
- _ Str _ Method
- Output content when the object is output (must be a string)
- PS (in Python, a pair of brackets can automatically help with the connection when the line is changed)
4. Identity Operators
Compare whether the memory addresses of two objects are consistent-whether to reference the same object
When comparing None in Python, we recommend that you use is to judge
- Is similar to id (x) = id (y)
- Is not similar to id (x) = id (y)
3. Private attributes and Methods
1. Definition Method
When defining attributes and methods, add two underscores before the attribute name and method name to define private attributes or methods.
2. Pseudo-Private attributes and private methods
Python does not really mean private
In fact, it only makes some special processing for the name, making it inaccessible to the outside world.
Processing Method: Add _ Class Name (I .e. _ class name_name) before the name