Python syntax learning functions, classes, modules

Source: Internet
Author: User

Python implements the programming of object-oriented (object-oriented programming, or OOP) by using classes (Class) and objects (object).

The main purpose of object-oriented programming is to improve the reuse of the program, which is similar to the purpose of the function.

One: function

If you need to reuse code in your program,---------define the function. def function name (parameter)://todo

Such as:

Output:

You can also define the parameters of the function as default parameters, note: The default parameter is usually the last parameter, such as:

  

Output:

II: Class

The definition of a class should be placed in the object-oriented title, but our general function is defined in the class, and from this scope I record the class.

Classes and objects are the two main aspects of object-oriented programming. class: creates a new type, and the object is an instance of this class, and the class is created using the class keyword. The fields and methods of the class are listed in an indent block.

Note: In the Python language, no matter what type of instance is considered an object, such as an integer is also treated as an object, it belongs to the Int class, which is different from the other languages C++,java the integer purely as a type.

The concept of "domain":

A variable that belongs to an object or class is called a domain, which is actually a variable defined within a class .

Domain -- Variables and objects of the class

1 : variables of class: all objects (instances) of a class are shared, with only one copy of the class variable, so when an object changes the class's variables, the change is reflected on all other instances. I understand that it is actually a global variable of a class that can be called by an object after the class is instantiated.

2 : The variable of the object: owned by each object/instance of the class . So each object has its own copy of the domain, that is, they are not shared, and in different instances of the same class, although the variables of the object have the same name, they are unrelated. I understand that: different objects call this variable, the value of the change does not affect each other, like C # syntax, I also because do not understand the 1th tangled half a day!

A variable at the class level and must take a type name when using it like Myclass.count .

variables that belong to each object level, always take the self when calling indicates that it belongs to the current object. Self.name

Such as:

Output:

Fields (variables) and methods (functions) can be combined as properties of a class

methods of the class :

There is only one special difference between a method of a class and a normal function -- They must have an additional first parameter name , but you do not have to assign a value to this parameter when calling this method , Python will provide this value. This particular variable refers to the object itself, by convention its name is self . (Similar to C # 's this pointer)

Such as:

__init__ method : A constructor that belongs to the Python language, and a class can have only one __init__ method

__del__方法属于python语言的析构函数,It is called when the object dies.

The method runs when the object is no longer in use, __del__ but it is difficult to guarantee when the method will run. If you want to indicate its operation, you have to use the del statement

Three: module

Module: If you want to reuse many functions in other programs--------define the module. is actually a lot of types, a lot of methods are set in one or more files, loaded by import * *, similar to DLL in C #

A module is basically a file that contains all of the functions and variables you define. In order to reuse modules in other programs, the file name of the module must be the .py extension.

Note: Each module has its own name __name__, __name__ as the module's built-in properties, its role is to detect how to call the. py file, and then make the appropriate processing according to __NAME__.

There are two ways to call a module: 1: Import loaded in Call 2: direct use

If the module executes directly, __name__= "__main__"; Typically, this statement is used in module tests.

For example: Define a module with the name mymodule.py

Direct Run output:

Call through other modules:

Calling module code:

Indirect call Run Output:

Import some classes, functions and variables, you can use From...import ... For example from MyModule import Moduleclass

Python syntax learning functions, classes, modules

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.