Python constructor, Python destructor, Python garbage collection mechanism

Source: Internet
Author: User

Constructor and destructor Constructor: used to initialize the state of the content of the class, the constructor provided by Python __init __ (); __init __ () method is optional, if not provided, Python will give the default __init_ _Method General data acquisition needs to define the get and set method destructors: Used to release the resources occupied by the object, the destructor function provided by Python __del __ (); __del __ () is also optional, if not provided Python will provide a default destructor in the background. If you want to call the destructor explicitly, you can use the del keyword as follows: del object name garbage collection mechanism Python uses a garbage collection mechanism to clean up objects that are no longer used; Python provides gc The module releases objects that are no longer in use. Python uses the 'reference counting' algorithm to handle recycling, that is: when an object is no longer referenced by other objects in its scope, Python automatically clears the object; Python's function collect () Can collect all pending objects at once (gc.collect ())
-------------------------Dividing line----------------------- ———————————

One: function

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

  Such as:

Output:

   You can also define the parameters of the function as default parameters. Note: The default parameters are generally placed at the end of the parameters, such as:



Output:

Two: class

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

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

Note: In the Python language, no matter what type of instance is regarded as an object, such as integers are also treated as objects, it belongs to the int class, which is different from other languages c ++, java, which treats integers as pure types.

The concept of "domain":

Variables belonging to an object or class are called domains, which are actually variables defined in the class

Domains-variables of classes and variables of objects

1: Class variables: shared by all objects (instances) of a class, there is only one copy of the class variables, so when an object makes changes to the class variables, this change will be reflected on all other instances . I understand it: in fact, it is a global variable of a class, and all objects instantiated by the class can call the variable.

2: Variables of objects: owned by each object / instance of the class. Therefore, each object has its own copy of this field, that is, they are not shared. In different instances of the same class, although the variables of the object have the same name, they are not related to each other. What I understand is: the variable is called by different objects, and its value does not affect each other after being changed. Like the syntax of C #, I have been entangled for a long time because I do n’t understand the first point!

Variables belonging to the class level, when using it, you must bring the type name such as MyClass.count

Variables belonging to each object level must be brought with self to indicate that they belong to the current object. self.name

Such as:


Output:

 

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

Class methods:

There is only one special difference between class methods and ordinary functions-they must have an additional first parameter name, but you do not assign a value to this parameter when calling this method, Python will provide this value. This particular variable refers to the object itself, and by convention its name is self. (Similar to this pointer in c #)

Such as:


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

The __del__ method: belongs to the destructor of the Python language, which is called when the object is gone.

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

Three: Module

Modules: If you want to reuse many functions in other programs -------- define modules. In fact, there are many types, many methods are collected in one or more files, loaded by import **, similar to the dll in c #

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

Note: Each module has its own name __name__, __name__ as the built-in attribute of the module, its role is to detect the calling method of the .py file, and then make the corresponding processing according to __name__.

There are two ways to call the module: 1: Loaded by import and called 2: Used directly

If the module is executed directly, __name __ = "__ main__"; Generally, this statement is used in module testing.

For example: define a module name as MyModule.py


Direct operation output:

Called through other modules:

Call the module code:

Indirectly call the running output:

 

To import some classes, functions and variables, you can use from ... import ... For example from MyModule import ModuleClass

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.