(This article is a supplement to the ordinary road (7), etc.)
First, dynamic import module
import
importlib
__import__
(
‘import_lib.metaclass‘
)
#这是解释器自己内部用的
#importlib.import_module(‘import_lib.metaclass‘) #与上面这句效果一样,官方建议用这个(亲测可用)
Second, abnormal error raise use
Throwing exceptions using raise
when an error occurs in the program, Python automatically throws an exception, or it can throw an exception by raise the display. Once the raise statement is executed, the statement following the raise cannot be executed.
Demo Raise UsageTry:s = Noneif S is None:print "s is empty object"raise Nameerror #如果引发NameError异常, the following code will not executeprint Len (s)except TypeError:print "Empty object has no length"
Iii. Use of assertions
used to detect whether a condition expression is true. An Assert statement is also known as an assertion statement, which asserts that an expression detected by Assert is always true and that conditional judgments in an If statement can be detected using Assert statements. If you are very confident that there is at least one element in the list you are using, and you want to test this and throw an error when it is not true, then the assert
statement is the ideal statement to apply in this case. When the Assert statement fails, one is raised AssertionError
.
Assertion 1.py#!/usr/bin/env python#Author is Wspikh
#-*-coding:encoding-*-
Import SYS
"" " def k (x):
x = x + 1
return x
y= K (5)
#断言错误
assert Type (y) is strprint (y)Assertion 2.py#!/usr/bin/env python#Author is Wspikh#-*-coding:encoding-*-
A =
Print (a)
Assert a < 30
A + = 24
Print (a)
assert a <
Four, sticky bag related
Five, the socket of the advanced
The ordinary way of Python (8)