The object-oriented, Class concept and use of Python

Source: Internet
Author: User

Basic concepts of Class (classes):


    • Class: An expression that abstracts the common denominator of the same particular transaction, and the Python class is a callable object.

    • There are methods, variables, and data structures defined in the class.

    • The variable is that the Class property itself has no data, and the data is introduced only when it is instantiated.

    • Methods are functions, code that manipulate the data referenced by the variables, know that the method is manipulating the data, because the class does not contain data, so the class cannot invoke the method, and only the instantiated ingest data becomes an instance (also known as an object) to invoke the method.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/68/wKioL1W3YdqgRLUdAAJpUMeMzr4546.jpg "title=" 2--2. JPG "alt=" wkiol1w3ydqgrludaajpumemzr4546.jpg "/>

The origin of the object:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/6B/wKiom1W3U_6wTo4rAABqBLMXMjw257.jpg "title=" Qq20150727202931.png "alt=" Wkiom1w3u_6wto4raabqblmxmjw257.jpg "/>

An object (also known as an instance) itself is not a method, and an object reference method is also called a class function, or interface, defined in a class.

A class of instantiated objects that have the same functionality.


The execution scenario for the class:

When a class is defined, the code in the class is not executed and is executed only when instantiated. The methods of the class do not execute during instantiation, only when the instance invokes the method.


The class object is a callable object, and the instance object is a method that can be called.


Inter-class relationships:

Dependency: The method of one class operates on the object of another class, and if the object of another class is not instantiated, the class cannot be executed.


Aggregation: An object of Class A contains an object of Class B


Inheritance: Describing special and general relationships


The 3 principles of an object-oriented model mechanism:

Encapsulation (Package)

A programming mechanism that hides the implementation details and binds the code and its processed data to ensure that programs and data are not externally disturbed and are not misused.


Inheritance (inheritance)

By inheritance, a subclass object can obtain the properties of a parent class object, and a subclass of deep inheritance inherits all the properties of its ancestors in the class hierarchy. (Superclass, parent, base class) are the same concept

When a property is called in a subclass, if it is not in this class, it will always look up to the parent class.


Polymorphism (polymorphism)

A feature that allows an interface to be used by multiple generic class actions, specifically using which action is relevant to the application, which is equivalent to multiple methods of an interface.

Concept understanding: For a class, when it is passed to it is different data, but can complete a number of different operations. In order to achieve the same purpose, and define the same interface, there are many different implementations behind it. This mechanism, which automatically determines which implementation to invoke, is called polymorphic.

Function: Reduce the complexity of the program


Start a class program:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/6B/wKiom1W3Y2jD-xxbAAKhg-tD9cw397.jpg "title=" 3.png " alt= "Wkiom1w3y2jd-xxbaakhg-td9cw397.jpg"/>


The self in the class method is used to identify the instance itself. Any class method should start with self.


Invoking a method through an instance is equivalent to invoking the method of the owning class to handle the current instance

in [+]: ins1.setdata (' Hello Python class ') in []: Ins1.printdata () Hello Python class

That's the same as writing.

In [the]: Testclass.setdata (ins1, ' aabbcc111222333 ') in []: Testclass.printdata (ins1) aabbcc111222333


Before the SetData method call, the MyClass class does not append the Name property to the instance ins1, or you can overload the __init__ create constructor to provide the instance directly.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/6B/wKiom1W3Zi7gup3FAAFxzwTS0TE865.jpg "title=" 4.png " alt= "Wkiom1w3zi7gup3faafxzwts0te865.jpg"/>


Python Constructors (aliases: construction methods, constructors)

In Python, each class has a constructor method __init__, which takes a parameter from the class name to the class method at instantiation, so that it can be passed directly to the method when the class is initialized.


The constructor constructs the instance property directly in the Shishun instance.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/6C/wKiom1W3Z3LDXBLoAAH_OJbmS8M620.jpg "title=" 5.png " alt= "Wkiom1w3z3ldxbloaah_ojbms8m620.jpg"/>


Constructors are the most convenient way to create instance properties


__VARNAME__: In some scenarios, the Python interpreter will call them automatically.

A.__add__ (b) is automatically called when executing a + B program

The program automatically calls list.__init__ () when executing l2 = [+/-]


destructor: Used to destroy an instance, the interpreter is automatically garbage collected, so destructors are rarely used.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/6C/wKiom1W3atGgKcSUAAG9uXk1iN4070.jpg "title=" destructor. png "alt=" Wkiom1w3atggkcsuaag9uxk1in4070.jpg "/>


Gets all available properties and available methods in the class, either using the dictionary property of the __dict__ class or the built-in function dir.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/69/wKioL1W3bVqCwhcgAAEtQ3Xk-6g291.jpg "title=" Class dictionary. png "alt=" wkiol1w3bvqcwhcgaaetq3xk-6g291.jpg "/>

-------------------------------------------------------------------------------------------

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/6C/wKiom1W3a6LCbGTZAAFV59FFboM654.jpg "title=" Class dictionary 2.png "alt=" Wkiom1w3a6lcbgtzaafv59ffbom654.jpg "/>

Get instance properties and methods

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/69/wKioL1W3bcqyNI1bAAC60lyO2j8947.jpg "title=" Instance properties. png "alt=" wkiol1w3bcqyni1baac60lyo2j8947.jpg "/>


Available variables for the method

Instance variable: Specify the variable name and the instance itself to refer to self. Variable name

Local variable: A variable created inside a method that can be used directly

Class variable (also called static variable): Reference by specifying the variable name and the class name

Global variables: Direct use

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/6C/wKiom1W3bULjGrzpAAGJqFY4zNo726.jpg "title=" 123. PNG "alt=" wkiom1w3buljgrzpaagjqfy4zno726.jpg "/>


Inheritance of Classes

Inheritance describes how the properties of a base class are "inherited" to subclasses, which can inherit any property of its base class, including data properties and methods, a class that does not specify a base class, which has a base class named object by default, and Python allows multiple inheritance to inherit multiple parent classes in parallel.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/6C/wKiom1W3blPw486IAAD0XjdhrQM687.jpg "title=" inherit. png "alt=" Wkiom1w3blpw486iaad0xjdhrqm687.jpg "/>


Implementing subclass Inheritance Construction methods

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/69/wKioL1W3cJ-SDbimAALkSkEpCk4571.jpg "title=" Inheritance constructor method. png "alt=" wkiol1w3cj-sdbimaalkskepck4571.jpg "/>


Almost all of the properties in Python can be obtained using the format "Object.attribute".

This expression launches a search —— —— a contiguous tree in Python

The class statement produces an object, and the call to class creates an instance, and the instance is automatically connected to the class that created the instance.


How classes are connected to their superclass

Inheritance is from bottom to top, searching this tree from left to right to find the lowest position that the attribute name appears.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/6C/wKiom1W3b4vgJ5rfAABJjv6pAgQ952.jpg "title=" The lookup relationship of the instance to the class. png "alt=" wkiom1w3b4vgj5rfaabjjv6pagq952.jpg "/>


Specialization of inheritance methods

Inheritance will look for the variable name in the subclass before looking for the superclass, so the subclass can override the inherited method by overriding the properties of the superclass. You can also extend the method of a superclass by using overridden methods to callback a superclass

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/6C/wKiom1W3cGLQl77PAAHsWCF0uyE785.jpg "title=" Inheritance looks for the variable name in the subclass first. png "alt=" wkiom1w3cglql77paahswcf0uye785.jpg "/>

Built-in functions for classes, instances, and other objects

Issubclass ()

Boolean function that determines whether a class is a subclass of another class

In []: Issubclass (Two_class,one_class) out[38]: True

Isinstance

Determines whether an object is an instance of a class

In [All]: Isinstance (Ins2,two_class) out[42]: True

Hasattr ()

Determines whether an object (instance) has the specified property

in [+]: hasattr (ins2, ' Data_one ') out[46]: Truein [[hasattr]: ins2 (+, ' data_two ') out[47]: True


This article from "Breakthrough Comfort zone" blog, reproduced please contact the author!

The object-oriented, Class concept and use of Python

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.