Python Object-oriented programming

Source: Internet
Author: User

    1. Java and C # only support object-oriented programming, and Python can support object-oriented or functional programming.

    2. Object-oriented Two important concepts: 1. Class 2. Objects

    3. Functional def sendemail (EMAIL,MSG): Functions

      Print ("8888")

    4. Object-oriented class Myclass:

      def sendmail (self,email,msg): function definition in a class The first argument must be self

The print ("8888") function defines the called method in the class


5. Object-oriented first create the class and then create the object. A method in a class can only be called by an object of the class.

6. The essence of the first parameter self in an object-oriented method

Class SQLHelper:

def __init__ (self):

Pass

def query (Self,sql):

Print (Self.host)

Obj1 = SQLHelper ()///When a class name is appended with a parenthesis, the __ini__ () method in the class is automatically called

Obj1.host= "1.1"

Obj1.query (SQL)//obj1 calls the Query method in Sqlhepler, the first parameter in the Query method is the self representation obj1

Obj2 = SQLHelper ()

Obj2.host = "1.2"

Obj2.query (SQL)//obj2 calls the Query method in Sqlhepler, the first parameter in the Query method is the self representation obj2


The method called in the 7.python class does not have to be defined before the method is called (regardless of the order in which the method is defined, _init_ is performed first),

This is not the same as Python-functional programming (the called function must be defined after the called function, and the code executes sequentially from the top).


8. Functional application Scenarios--independent and no shared data between functions

Object-oriented scenario-there are a number of repeated calls between functions

Object-oriented encapsulation, in fact, is to use the construction method to encapsulate the content into the object, and then directly or self indirectly through the object

Get the encapsulated content


9. polymorphic

Parameters in a function in Python do not need to specify parameter types to receive arbitrary types of parameters, and automatically implement polymorphic

Other languages are inherited by the class, and the arguments in the function must be of the specified type or subtypes of the specified type to implement polymorphism

10.


This article from "Number One" blog, declined reprint!

Python Object-oriented programming

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.