Basic knowledge of Python - flow control, functions, objects, classes, and garbage collection

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/suoyue_py/article/details/99710036

The basic process flow

It refers to the process flow of the data structure. In Python, it comprises three basic processing flow structure

  • Sequence structure: according to the order of the statements appear sequentially executed
  • Selection structure: according to a predetermined logical conditions to determine the execution order, selecting a one-way, two-way selection, and the selection of multiple points, but only the program execution where a branch during execution.
  • Loop structure: The condition code logic determines whether a certain period of the program is repeatedly executed, if the logical condition is true, the process proceeds to repeat the loop, the loop ends otherwise. Loop structure can be divided into cycles and counting cycle conditions.

if-elif-else conditional statement
while loop: can be used with else, when the while statement indicates when the expression is False, the else of the block.
for loop: the controlled conditions and the circulation of two parts
syntax:

for <variable> in <sequence>:
   语句
else:
   语句

Where the variable is a variable name, sequence is a list of
time to run when the else statement for statement are not running, or the last cycle has run. else statement can be omitted.
Binding loop often continue (block skip the remaining statements in the current cycle, continue to the next cycle) and BREAK (out of the loop) statement
pass statement is empty, the main program in order to maintain the integrity of the structure. pass without doing anything, generally used as a placeholder statement.
If the sequence numbers need to be traversed are typically used range () and len () function, binding loop control statements, will play a multiplier effect.

function

Python is to use the function definition def keyword syntax as follows:

def function name (parameter 1, parameter 2, ...):
"File string"
<statement>

File string can be omitted for this function is described as a string. If the file exists, then the string, it must be the first one statement function.
Python function parameter passing , are using pass-call mode. The so-called pass-by-call is the memory address of the parameter passed in the past, if the parameter is changed within the function, the original parameters will affect. Data type of the parameter may be a module, class, instance (instance), or other functions, the user need not set the data type parameter in the parameters. Parameter type can be used when calling functions include the required parameters, keyword parameters, default parameters and variable length parameters.
The return statement is used to exit function, returning a selective expression to the caller. Return statement with no parameter values return None.
After loading the Python interpreter, the reader can directly use the built-in functions . The following will describe the common use of built-in functions.
ABS (x): Returns the value of the absolute value of x, if x is a plural number, ABS () function returns the (real part squared plus the imaginary number part of the square, then square root) the size of the complex. For example:
>>> ABS (-3.12)
3.12
>>> ABS (. 1 + 2J)
2.23606797749979
CHR (i): i is an ASCII character code (0 ~ 255), chr ( ) function returns the value i of the single-character string . CHR () function and the ord () function is the opposite effect. Case ASCII character code is obtained following 97 characters:
>>> CHR (97)
'A'
built-in properties and functions namespace
There are many functions built-in property, the user can enter the Python interpreter dir (function name), which can display the built-in properties.
Input and output functions
Python built-in function input () and print () function is used to input and output data.
print () function is the default output a line feed, to achieve the end does not wrap can be added at the end of the variable = "."
Format: print (output data, end = "")

Key features of object-oriented programming

  • Package (encapsulation): the data can only be accessed via a set of interface functions through the encapsulated data to ensure privacy information.
  • Inheritance (inheritance): derived class (derived class) inherits its base class (Base
    class) member variables (data member) and class methods (class
    Method). The derived class times also called class (subclass), or subclass (child class). Foundation Classes also called the parent class (parent class).
  • Polymorphism (polymorphism): a polymorphic function is allowed, there are many different types of interfaces. In accordance with the parameters used when calling functions, classes, know what kind of interface. Python uses dynamic typing (Dynamic
    Typing), and late binding (late binding) to do polymorphic function.

Class (class)

A group of objects with the same properties and the same behavior is called class (class). Broadly speaking, things have a common set of properties is called class.
In object-oriented programming, it is a class independent unit, which has a class name, its interior member variables for the object attribute description; further comprising a class member methods , for describing the behavior of the object.
Class is an abstract concept, like to take advantage of the way to solve the problem, you must create an instance of the object with the class, and then through the object to access the class member variables, to call for class members to implement the functions of the program.
Each class has its own namespace, all the settings and function definitions occur within this namespace.

The class definition
to use the package data, and the operation of the data interface function, the set consisting of a group of objects. Class can be said is that when you create the template used by the object (template). Python uses the keyword class to define the class.
The syntax is as follows:

class <类名称>:
  [“文件字符串”]
<语句>

<Statement> contain any valid statements within Python, attributes and methods defined for the class. "File string" is a string of such statements can be omitted.

Construction methods and built-in properties like
the so-called constructor (constructor) is that when you create an object, the object itself running function. Python uses __init __ () as a function of object constructor. When a user wants to point to objects within the object itself, you can use the self keyword. Like the self keyword Python and C ++ this keyword, they are representative of the object itself.
Python all classes have the following properties:
A, .__ dict __: properties of a dictionary object is stored in the attribute value of the dictionary object
B, .__ DOC __: property returns the class string of the file
C, .__ name __: property returns the name of the class
D, .__ Module1 __: property returns the name of the module containing the class
E, .__ bases __: attribute is a tuple object, and returns the name of the base class

Built-in class methods
class itself has many built-in method, the beginning and end of these built-in methods are dual bottom line of characters.
A, __ __ the init (Self): This is the class constructor when creating an instance of the class, this method is called.
B, __ __ STR (Self): This method is str () built-in function and the print function call. To set the object into a string appears how to display the type, STR () function return value is a string object.
C, __ __ the repr (Self): This method is the repr () built-in function calls, this function allows an object appearing in a readable form
D, __ __ getattr (Self, name): This method is used in a read or modified It does not exist when the member properties.

Class instance
To create an instance of the class, as long as the specified variable to the class name.
Use ⑴ id () built-in function that returns the class identifier (identity).
⑵ use type () built-in function that returns the object type (object type) class.
All Python class instance, have a built-in property of the following:
⑴ obj. Dict : properties of an instance of the class is stored dictionary objects. __dict__ value for this attribute dictionary object.
Obj ⑵. Class : __ class__ property returns the name of the class to create such examples used.

Overload operator (operator Overloading)
built-in methods to replace class operator characteristic function
to be used in the operator function Python interpreter must first load the operator module , then call operator function operator module.
Here Insert Picture Description
Class inheritance (inheritance)
is the new class inherits the properties and methods of the old class, this behavior is called a subclass (subclassing). The new class called inheritance derived class (derived class), inherited the old class is called the base class (base class). When the user creates a derived class, you can add or rewrite any of the methods of the base class in the derived class.
A derived class syntax is as follows:

class <类名称> [(基类1,基类2, ...)]:
  [“文件字符串”]
<语句>

A derived class may inherit the same time (,) separated by commas from among a plurality of base class, the base class.
Namespace search order: the instance of the class -> Class -> base class

Polymorphic class (polymorphism)
is a class can have a plurality of the same name, but different types of function parameters.
Python is no obvious polymorphism characteristic, because the parameters do not have to Python functions declared data type. However, dynamic data type (dynamic typing), Python still process polymorphic objects.
The use of dynamic data types, Python must wait until the function is run only know the type of the function. This feature is called run-time binding (runtime binding).
The C ++ polymorphism (polymorphism) method called reload (method overloading), C ++ allows a plurality of the same name in the class, there exists a function of different parameters, Python is not allowed, the last statement will use the class function

Encapsulation class (encapsulation)
is attribute of its class (variables and methods) housed within the class. Only members of this class, you can use the other members of the class. Such variables and packaging method, called the class variable private (private variable) methods with the private (private method).
All variables and class methods Python, are common (public). Only know the name of the class, and the name of the variable or method of any external object attributes and methods can be directly accessed class.
As shown in the following example, x is an instance variable myClass class, name is a variable myClass class. X.name can access using the variable name myClass class.

>>> class myClass:
      def __init__(self):
         self.name = None

Class encapsulates the two principles:

  • The first internal variable character attributes (variables and methods) If the name of a single bottom line, this property regarded as the class of the external variables can not be referenced this property
  • The first two characters attributes (variables and methods) are single-name if the bottom line is when compiled with the attributeName attribute name will be changed _className__attributeName, className is the name of the class. As the name of the class before adding the attribute name, so there are differences in class and any original property name.

Yuan class
is a class used to create other types of templates (template) as a. In general, users use the class to create the class instance variables. The purpose of using certain types metaclass is the basis to create metaclass class. Then yuan class as the base class to be created.
Use metaclass allows users to have the opportunity to access and change the internal class Python. Examples of the use of RMB yuan class is created, it can make work easier operation object properties.

Garbage Collection

Python uses reference counting this simple technology to track and recover waste.
Within Python, it has an internal tracking variables, a record of how many references to the use of all the objects in each, called a reference counter.
When an object is created, it creates a reference count, when the object is no longer needed, the object's reference count goes to zero, it is garbage collected. But the recovery is not "immediately" by the interpreter at the appropriate time, it will take up memory space garbage objects recovered.

Guess you like

Origin blog.csdn.net/suoyue_py/article/details/99710036