Python's OOP-oriented object-based programming basics

Source: Internet
Author: User

O bjects

Sorts of program Languages

Machine orinted

Machine instructions

Assembly

Procedure orinted

Follow steps and branch processing

C

Object orinted

Multi-person, multi-departmental writing

C + + Java Python

    • Everything in Python, object-oriented, very thorough.

    • Objects are encapsulated in data and operations

    • Objects are independent, but objects can interact with each other

    • At present, OOP is the closest programming paradigm to human cognition.

Oop

Description Data--property

Action--method

Class

Collection of profiles

There are no functions in the class, called methods.

Instance Object

Three elements of OOP:

1. Package:

Assembled and closed.

Data is hidden.

2. Inheritance:

Multiple multiplexing

Multiple inheritance less modification, OCP (open-closed Principle)

3, polymorphic:

Dynamic binding. Python almost doesn't exist

Python's class

class MyClass:

"" " A Example Class " ""

    ' abc '   # Class Property

    foo ( self ):  foo is method object

Print(self. x) , do not swap, pass the instance of the class itself as a method parameter

Print (MyClass)

Print (MyClass. __name__ )

Print (myclass.x)

Print ((myclass.foo))

<class ' __main__. MyClass ' >

MyClass

Abc

<function Myclass.foo at 0x00000000011cb840>

‘.’ It was used to find members.

. __dict__ you can get the dict that contains all the properties of MyClass, including the one we defined

X variable

__doc__: "" "A Example Class" ""

Foo method

and a default __module__:__main__.

__init__ Methods and other properties

Class objects and Class properties

    • Class Object , define a class that is a class object type class

    • properties of the class , the variables and methods in the definition are the properties of the class. All belong to the identifier of the class

    • class Variables , in the example above, X is a variable of class MYCLASSD

Foo is a Methond method object, not a normal fucntion, must have at least one argument, and the first argument must be self.

Self refers to the current instance itself

mycls = MyClass () # Instantiate, Initialize

Print (Mycls.foo ()))

Print (mycls.x)

Print (Mycls.foo)

Abc

None

Abc

<bound method Myclass.foo of <__main__. MyClass Object at 0x00000000011e57f0>>

Class

Print (MyClass)

    • Instantiation of

A = MyClass ()

After the class object is added (), the instantiation method of the calling class is invoked to complete the instantiation.

When the Python class is instantiated, the __init__ method is called automatically, and the first argument must be left to self.

def __new__ instantiation

    • __init__ method

__init__ (self), initialization method, also called constructor, constructor method,

has been constructed to complete

Can only return none,

    • Instance

    1. Object after the class instantiation is an instance object

    2. An instance variable is each instance of its own variable, a class variable is a variable of a class, and is a property and method shared by all instances of the class.

    3. Class properties are stored in the __dict__ of the class, the instance properties are stored in the-__dict__ of the instance, and if the properties of the class are accessed from the instance, the owning class needs to be found with __class__.

__qualname__

Class type instances have qualified name __qualname__, not equal to name

__dict__ Property Collection

Lookup Order of Instance properties

The object operation of the function is the same

First look at your own dict, and then through the property __class__ find their own class and then go to the class of dict find.

Decorate a class

Call class with __xxxx__

    • Is the class is everyone's.

    • If you don't have a reference, use @staticmethod

Def STATICMTD ():

Print (' static ')

Access control public Protect private

Private property

_person__age can be found in the __dict__ of an instance because the interpreter is renamed

Protect Property

_name is the developer's agreement, do not change

Private method, similar to private variables

In the __dict__ of the class can be found


This article is from the "Python" blog, so be sure to keep this source http://13320196.blog.51cto.com/13310196/1979407

Python's OOP-oriented object-based programming basics

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.