The basic theory
What is the object
Everything Is an Object
Objects are specific things
- Have attributes, behaviors
- The many scattered things, the package as a whole
Everything in Python is an object, Python is a particularly thorough language of object-oriented programming (OOP)
Many other programming languages also points primitive types and object types, while in python, only the object type
& Object-oriented process-oriented
Both are problem-solving ideas, thoughts, object-oriented itself is a process-oriented package
- Process-oriented: In solving problems, attention is to solve the problem of every process (step)
- Follow the steps in division
- Put a task is decomposed into each of the specific steps
- Object-oriented: In solving problems, attention is needed to solve the problem objects
- Divided according to functional objects
- Find the object, to determine the properties and behavior of objects
- How object from process-oriented to face:
- Task Steps list
- And object separation step function block
- The function code is divided into specific object
- The specific behavior of objects and, based extract
class
What is the class
Abstract class is a particular class of object features
The role of class
The abstract class, the production of specific objects
The composition of the class
Name, properties, methods
note:
- Properties and methods, are abstract concepts
- After generating the object, the object has only a specific attribute values, and means to achieve
The relationship between objects and classes
Objects - Abstract - class
Class - Examples of - objects
The definition of class
Classic definition:
class 类名:
......
Create an object
obj = 类名()
The underlying operation when creating an object:
- Will first open up some space in memory (define classes will take up memory)
- Get the class name through the class variables:
xxx.__name__
- Get the class through an object:
xxx.__class__
Property related
The difference between attributes and variables
concept:
- Variables: value can be changed
- Property is: are the characteristics of an object
access permission:
- Variables: Depending on the scope, there are different access rights
- Properties: you can only be accessed through the object
- The object is referenced by variables, scopes also have rights
Judgments based:
- Whether there is a host
Add property
- Directly through the object, add the dynamic
- grammar:
对象.属性 = 值
- grammar:
- Initialization method (constructor) by class
__init__()
With the addition of a property, to open up some space in memory to store the property value, then this value is stored in the object space where the object reference variables
Access Properties
Directly 对象.属性
access
python provides a __dict__
property is a dictionary, which now contains all the properties of this object
Modify the properties
Modify property value:对象.属性 = 值
Delete property
del 对象.属性
Class Properties
Increase class attributes:
类名.属性 = 值
- In class, write directly
属性 = 值
View class properties:
类名.属性
- Class attributes can also be accessed via the object:
对象.类属性
- Object Properties discovery mechanisms:
- Priority to the object itself to find properties, find the end
- If not found, according to
__class__
find an object corresponding to the class, look for this class of property
Modify class properties:
- Modified by the class name:
类名.属性 = 值
对象.__class__.属性 = 值
- Note: You can not be modified directly through the object!
对象.属性 = xxx
Increasing property and modify the properties of the grammatical object
View all class attributes:类名.__dict__
Remove class attributes:
del 类名.属性
- Objects can not be deleted, del can only delete the object of direct property
Memory storage problem class attributes:
- Properties are stored in a dictionary, this dictionary is real, can
__dict__
be accessed 类.__dict__
It is a read-only property- to sum up:
- In general, the properties are stored in
__dict__
the dictionary, some built-in objects do not have this property - You can directly modify the general object
__dict__
attributes - But the class of the object
__dict__
is read-only, can not be modified by default, can be modified by means of setattr
- In general, the properties are stored in
Each object class attribute is shared: