Python syntax learning functions, classes, modules

Source: Internet
Author: User

I. Functions

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

For example:

Output:

You can also define function parameters as default parameters. Note: The default parameters are usually placed at the end of the parameter, for example:

  

Output:

Ii. Category

The class definition should be put in the object-oriented title, but we generally define the function in the class. In this range, I recorded the class here.

Class and object are two main aspects of object-oriented programming.Class:Create a new type, whileObjectIs an instance of this class, the class is created using the class keyword. Class fields and methods are listed in an indention block.

Note: In python, all types of instances are considered as objects, such as integers, which belong to the int class, java treats integers as different types.

Concept of "Domain:

A variable of an object or class is called a field.Variables defined in the class

Domain--Class variables and object variables

1:Class variables:Shared by all objects (instances) of a class,Only one class variable is copied, so when an object changes the class variable, this change will be reflected on all other instances. I understand that it is actually a global variable of the class, and all objects after the class is instantiated can call this variable.

2: Object variables:Owned by each object/instance of the class. Therefore, each object has its own copy of this domain, that is, they are not shared. In different instances of the same class, although the object variables have the same name, however, they are unrelated. I understand that different objects call this variable and their values do not affect each other. Like the C # syntax, I have struggled for a long time because I don't understand the first point!

A class-level variable must contain a type name when using it.For example, myclass. Count

Variables at each object level must contain selfIndicates that it belongs to the current object. Self. Name

For example:

Output:

 

Fields (variables) and methods (functions) can be collectively called class attributes.

Class Method:

Class MethodAndCommon functionsOnly one specialDifferences--They must have an additional first parameter nameBut when you call this methodNoAssign a value to this parameter. python will provide this value.This special variable refers to the object itself. By convention, its name isself. (Similar to the this pointer of C)

For example:

_ Init __Method: A python constructor. A class can have only one _ init _ method.

_ Del _ Method:Is a python destructor,It is called when the object disappears.

When an object is no longer used,__del__Method, but it is difficult to ensure that this method isWhenRun. If you want to specify how it runs, you have to usedelStatement

Iii. Module

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

The module is basically a file that contains all the functions and variables you define. To reuse a module in other programs, the module File NameRequiredTo.pyIs the extension.

Note:Each module has its own name__ name __,__ Name _ is the built-in attribute of the module. It is used to detect the call method of the. py file and then process it according to _ name.

The module can be called in two ways: 1: imported and called; 2: directly used.

If the module is executed directly, __name __= "_ main __";Generally, this statement is used in a module test.

For example, define a module named mymodule. py.

Run the output directly:

Call through other modules:

Call module code:

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.