Create a custom object
1, the object of magic
Object: a series of data (attributes) and a set of methods for accessing and manipulating data.
Polymorphism: the same operation can be performed on objects of different types of
Package: external hidden details about the object works
Inheritance: You can create a special class based on a common class
Method state and more than 1.1
Even if you do not know what kind of variable points of the object, its behavior can also perform an operation, and the operation of the type (class) with the object belongs to another.
Function and object properties associated with the method is called.
1.2 Packaging and Properties
Property is attributable to variable objects
Attribute describes the status of the object
Object attributes can be modified method
Summary: The object is a set of functions (methods) are combined, and give them access to some variable (property) rights, and the property can be used to store a value between function calls twice.
1.3 Inheritance
Subclass has all the attributes and methods of the parent class, and which method is easy and or properties of the parent class
2, class
What class is 2.1
Class - An Object.
Each object belongs to a particular class, called the class and instance .
Methods supported by the class definition.
Subclass (subclass)
Superclass (superclass)
2.2 Creating a custom class
2.3 Properties, functions and methods
The method (method associated) to the first parameter (self) instance associated with it belongs, there is no need to provide this parameter.
2.4 Hide
Let the method or property called private (not accessible from the outside), to name just let the two can begin with an underscore.
Private property can not be accessed from an external object, but only by the accessor to access method (e.g. get_name and set_name).
Note :
In the class definition, all names beginning with two underscores are converted, namely adding an underscore and the class name at the beginning, will be able to access private methods from outside the class.
If you do not want the name to be modified, issued do not want to modify the properties of the access method or from an external signal, can begin with an underscore, this is a convention.
Namespace class 2.5
Code is defined in the class statement is within a special namespace (like namespaces) execution, and all members of the class can access the namespace.
2.6 specify the superclass
To specify a super class, the class name may be in the class statement, plus the name of the superclass, and enclosed in parentheses.
2.7 depth discussion of inheritance
To discuss whether a class is a subclass of another class, the method may be used built issubclass
If you have a class, visit its special properties __bases__ know that it is the base class
To determine whether an object is an instance of a particular class, may be used isinstance
If you want to know if the objects which belong to the class, you can use the property __class__
type (s) may be informed of the class to which it belongs
More than 2.8 superclass
Multiple inheritance.
A plurality of the same superclass superclass, sequential access method called superclass resolution order (MRO) when looking for a particular method or property.
A common practice is to use a core of super class and one or more mixed superclass.
2.9 Interface and introspection
Objects follow specific interface (i.e., a specific method implemented)
2.10 abstract base class
Python support an abstract base class called by introducing a module abc.
3, object-oriented design
To determine what classes and these classes which methods should be included, you can do this:
1, the description of the problem (the program what to do) record, and give all the names, verbs and adjectives marked
2, identify possible class in terms of
3, to find possible ways of verb
4, identify possible attributes adjectives
5, the methods and attributes are assigned to identify each class
Improved Model
1, a recording (or scenario) with a series of cases.
2, thorough and carefully consider each scenario, ensure that the model contains everything you need.