Python Learning (vi): Object-oriented __python

Source: Internet
Author: User
object oriented and process orientedObject-oriented programming--object oriented programming, called OOP, is a kind of programming idea. OOP takes objects as the basic unit of a program, and an object contains functions of data and manipulating data. Object-oriented programming treats computer programs as a collection of objects, and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passed between objects. Sending a message to an object is actually calling the object's corresponding associated function, which we call the object's method. The object-oriented design idea is to abstract class and create instance according to class. Object-oriented abstraction is higher than function because a class contains both data and methods of manipulating data. Process-oriented programming treats a computer program as a series of command sets, the sequential execution of a set of functions. In order to simplify program design, process-oriented function is divided into sub functions, that is, to reduce the complexity of the system by cutting the block function into small function. Objects and InstancesThe most important concept of object-oriented is class and instance (Instance), which must keep in mind that the class is an abstract template, whereas an instance is a specific "object" created from a class, each object has the same method, but the individual data may be different. You can bind properties to an instance variable freely, because a class can act as a template, so when you create an instance, you can force some attributes that we think must be bound to fill in, by defining a particular Initmethod, when you create an instance, you bind the attribute to it. Have the Initmethod, you cannot pass in an empty argument when you create an instance, and you must pass in the Initmethod, but self does not need to preach, the Python interpreter will pass the instance variable in itself. The function defined in a class is only a little different than a normal function, that is, the first argument is always the instance variable self and, when invoked, does not pass the argument. In addition, there is no difference between a class method and a normal function, so you can still use default parameters, variable parameters, keyword parameters, and named keyword parameters. SummaryA class is a template that creates an instance, and an instance is a concrete object. Each instance has data that is independent of each other and does not affect each other; The method is the function that is bound to the instance, unlike the ordinary function, the method can directly access the data of the instance, and by invoking the method on the instance, we directly manipulate the data inside the object. But you don't need to know the implementation details within the method. Unlike static languages, Python allows you to bind any data to an instance variable, that is, for two instance variables, although they are different instances of the same class, the name of the variable may be different. Access RestrictionsIn Python, the instance's variable name, if it starts with __ (double underlines), becomes a private variable (private) that is accessible only internally and not externally. This ensures that the external code cannot arbitrarily modify the state within the object, so that the code is more robust by the protection of access restrictions. In Python, the variable name is similar XXX, which begins with a double underline, and ends with a double underline, is a special variable, a special variable that can be accessed directly, not a private variable, so when you define a variable, it is best not to variableThis form defines the class attribute. Sometimes you see an instance variable name that starts with an underscore, such as _name (an underscore), such an instance variable outside is accessible, but, according to the conventions, when you see such a variable, meaning, "although I can be accessed, however, please treat me as a private variable, do not randomly access ”。 Whether an instance variable beginning with a double underline must not be accessed from the outside. Not really. For example, the Student class and attribute __name cannot directly access __name because the Python interpreter has converted __name variables to _student__name, so it is still possible to access the __name variables through the latter. But it is highly recommended that you do not do this because different versions of the Python interpreter may change the __name to different variable names. In general, Python itself has no mechanism to stop you from doing bad things, all by itself. Encapsulation

The term object (object) in object-oriented programming can basically be viewed as a collection of data (attributes) and a series of methods that can access and manipulate the data. The traditional meaning of "program = data structure + algorithm" is encapsulated "masking" and simplified to "program = object + Message". An object is an instance of a class, and the abstraction of a class needs to be encapsulated. Encapsulation allows callers to use the object directly without concern about how it is built.Inheritance and polymorphismIn OOP programming, when we define a class, we can inherit from an existing class, the new class is called a subclass (subclass), and the inherited class is called a base class, a parent class, or a superclass (base class, Super Class). What is the benefit of inheritance. The biggest benefit is that subclasses get the full functionality of the parent class, and the second benefit of inheritance requires a little improvement in the code. In essence, inheritance maximizes the functionality of code reuse. Polymorphism: means that the variable does not know what the referenced object is, and behaves differently depending on the object being referenced. When dealing with polymorphic objects, you just need to focus on its interface. This is the famous "open and close" principle: opening to the expansion, that is, to allow the addition of new subclasses, to modify the closed, that is, the need to modify the interface method.Duck TypeA duck type is a style of dynamic type. In this style, an object's valid semantics are not inherited from a particular class or implement a specific interface, but rather by the "collection of current methods and properties." The concept was named after the duck test by James Whitcomb Riley, a duck test that says, "When you see a bird that walks like a duck, swims like a duck, and barks like a duck, then this bird can be called a duck." The duck type in Python allows us to use any object that provides the desired method without forcing it to become a subclass. The concept of duck type is very important in object-oriented design of State type language. With the idea of duck type, we can easily implement a principle in dynamic type languages without resorting to super type help: "Interface-oriented programming, not object-oriented programming." Because any object that provides the correct interface can be used interchangeably in Python, it reduces the need for polymorphic generic superclass. Inheritance can still be used to share code, but if all that is shared is a common interface, the duck type is all that is required. This reduces the need for inheritance and also reduces the need for multiple inheritance; Usually, when multiple inheritance seems to be an effective solution, we only need to use the duck type to simulate one of several superclass (the same interface and implementation as that superclass) is available. The character of duck type in dynamic language determines that inheritance is not necessarily the same as static language.Instance Properties vs Class PropertiesInstance properties: The way to bind a property to an instance is through an instance variable, or through the self variable; Class Property: If the class itself needs to bind a property. You can define a property directly in class, which is a property of the classes, all of which are categorized, and when we define a class attribute, this property, although it is all sorted, can be accessed by all instances of the class. When writing a program, do not use the same name for instance properties and class attributes, because instance properties of the same name will block out class attributes, but when you delete an instance attribute and then use the same name, the class attribute is accessed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.