So far, we've learned most of the common Python knowledge. In this chapter, we're going to learn some other aspects of Python knowledge to make our knowledge of Python more complete.
A special method
There are some special methods in the class that have special meanings, such as __init__
and __del__
methods, whose importance we have learned.
In general, special methods are used to imitate an action. For example, if you want to use x[key]
such an indexing operation for your class (like lists and tuples), you just need to implement the __getitem__()
method. Think about it, Python list
does this to the class!
Some useful special methods are listed in the table below. If you want to know all the special ways, you can find a huge list in the Python reference manual.
Table 15.1 Some special methods
Name |
Description |
__init__ (self,...) |
This method is invoked before the new object is exactly to be returned to use. |
__del__ (self) |
Called just before the object is to be deleted. |
__str__ (self) |
Called when we use statements to objects print or use them str() . |
__lt__ (Self,other) |
Called when the less-than operator (<) is used. Similarly, there are special methods for all operators (+,>, etc.). |
__getitem__ (Self,key) |
x[key] called when the index operator is used. |
__len__ (self) |
Called when a built-in function is used for a sequence object len() . |
Single statement block
Now, you have a deep understanding that each statement block is separated from the other blocks by its indentation level. This is true in most cases, however, but not 100% accurate. If your statement block contains only one statement, you can specify it in the same row of a conditional statement or Loop statement. The following example clearly illustrates this point:
>>> flag = True
>>> if flag: print 'Yes'
...
Yes
As you can see, a single statement is used directly rather than as a separate block. While doing so will make your program smaller, I strongly recommend that you do not use this method of thumb except for error checking. One of the main reasons not to use it is that once you use the appropriate indentation, you can easily add an extra statement.
Also, note that when using the Python interpreter in interactive mode, it will help you enter statements by appropriately changing the prompts. In the example above, when you enter a keyword, the if
python interpreter changes the prompt to ...
indicate that the statement is not finished. In this case, we press enter to confirm that the statement is complete. Python then completes the execution of the entire statement and returns the original prompt and waits for the next sentence to be entered.
List synthesis
With a list synthesis, you can export a new list from an existing list. For example, you have a list of numbers, and you want to get a corresponding list so that all the numbers greater than 2 are twice times the original. For this application, list synthesis is the ideal method.
Use list synthesis
Example 15.1 using list synthesis
#!/usr/bin/python
# Filename: list_comprehension.py
listone = [
2
,
3
,
4
]
listtwo = [
2
*i
for
i
in
listone
if
i >
2
]
print
listtwo