The ordinary way of Python (8)

Source: Internet
Author: User

(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)

Related Article

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.