Concise Python tutorial --- 15. More Python content
Special Method
Some methods have special meanings in the class, which has been mentioned earlier. For example, the _ init _ method and the _ del _ method.
These special methods are very useful. For example, you want your class objects to use indexes, such as OBJ [Key], you can implement the _ getitem _ method in your class.
Some special methods:
Name |
Description |
_ Init _ (self ,...) |
This method is called just before the newly created object is returned for use. |
_ Del _ (Self) |
It is called just before the object is deleted. |
_ STR _ (Self) |
When we use Print Statement or use STR () . |
_ LT _ (self, other) |
When using Less It is called when the operator (<) is used. Similarly, all operators (+,>, and so on) have special methods. |
_ Getitem _ (self, key) |
Use X [Key] It is called when the index operator is used. |
_ Len _ (Self) |
Use built-in Len () Function call. |
Single statement Block
In the previous introduction, you have seen that in Python, statement blocks are indented to indicate that they belong to the same level and are sub-layers of the previous level.
However, not all cases require line breaks and indentation. If a block contains only one statement. In addition, if the previous level is a condition statement or loop statement, the statement block can be written to the same row as the previous level. For example:
Flag = true;
If flag: Print 'flag is true .';
Comprehensive List
Through list synthesis, you can use a loop statement to initialize a list:
List1 = [1, 2, 5, 3, 7, 8];
List2 = [I for I in list1 if I> 3];
Allows the function to receive parameters of the list or tuples.
When you want a function to receive parameters in the form of tuples or dictionaries, there is a special method that uses the * and * prefixes respectively.
Def method (arg1, * ARGs)
Because there is a * prefix before The args variable, all redundant function parameters are stored as a tuple in args. If the prefix is **, redundant parameters are considered as Dictionary key/value pairs.
Lambda form
Lambda statements are used to create new function objects and return them at runtime.
Def method (prefix ):
ReturnLambdaS: prefix + ":" + S;
Newmethod = method ("ABC ");
Print newmethod ("111 ");
Print newmethod ("222 ");
Lambda statements are used to create function objects. Essentially, Lambda requires a parameter, followed by a single expression as the function body, and the value of the expression is returned by the newly created function.
Note that even print statements cannot be used in the lambda format, but expressions are only allowed.
Exec and eval statements
Exec statements are used to execute Python statements stored in strings or files.
For example, we can generate a string containing Python code at runtime, and then execute these statements using exec statements.
Exec 'print "Hello World "';
The eval statement is used to calculate the valid Python expression stored in the string. For example:
Eval ("2*3 ");
Assert statement
The assert statement is used to declare that a condition is true.
For example, if you are very sure that a list you are using has at least one element, and you want to test this and cause an error when it is not true, the assert statement is an ideal statement applied in this case.
When the assert statement fails, an assertionerror is thrown.
List = ["ABC"];
Assert Len (list)> 1;
Repr Function
The repr function is used to obtain the standard string representation of an object.
List = ["Item1", "item2"];
Print repr (list );
Run the preceding program and the output is ["Item1", "item2"].
Note: In most cases, Eval (Repr (object) = object. You can define the _ repr _ method of the class to control the content returned when your object is called by the Repr function.