十五. Python基础(15)--内置函数-1
十五. Python基礎(15)--內置函數-1
1 ● eval(), exec(), compile()
執行字符串數據類型的python代碼 檢測#import os 'import' in code ① eval : 有返回值, 適用于執行計算語句, 例如eval("4+3"). ② exec : 沒有返回值, 適用于執行流程控制語句, 例如exec(a = b if b>c else c) ③ complie: code1 = 'for i in range(0,3): print(i)' compile1 = compile(code1, '', 'exec') # 中間表示filename的參數即使沒有也要有一個空字符串(其實也可以胡亂寫一個字符串) print(compile1) # <code object <module> at 0x0000000002802270, file "", line 1> exec(compile1) exec(compile1) ''' 0 1 2 0 1 2 ''' |
compile(str ,filename ,kind ) compile()將一個字符串編譯為字節代碼, str是將要被編譯的字符串, filename是定義該字符串變量的文件, kind參數指定了代碼被編譯的類型: eval, single, exec 'eval'指一個表達式. 'single'指單個語句, 'exec'指多個語句, 返回一個代碼對象,該對象也可以被傳遞給eval()函數和exec語句來執行 ? 預編譯, 可以有效提高程序的執行效率 |
eval(), exec()和compile()不要隨便使用, 若使用, 也要做好充分的測試. |
?
2 ● print()函數擴展
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) |
print("枯藤", "老樹", "昏鴉", sep = ",", end = "←") # 枯藤,老樹,昏鴉← # sep默認為一個空格" ", end默認為一個換行符 |
import time for i in range(0,101,10): ???? time.sleep(1) # 便于觀察 ???? char_num = i//2???? #打印多少個'*' ???? #per_str = '\r%d%% : %s\n' % (i, '*' * char_num) if i == 100 else '\r%d%% : %s'%(i,'*'*char_num) ???? # 等價于: ???? #per_str = '\r%d%% : %s' % (i, '*' * char_num) if i != 100 else '\r%d%% : %s\n' % (i, '*' * char_num) ???? # 或者是: ???? per_str = '\r%d%% : %s' % (i, '*' * char_num) ???? # 但不能是: ???? print(per_str,end='', flush=True) ? # 100% : ************************************************** # 每次循環都是從頭開始打印在控制臺上的 |
f = open('print_test', 'a', encoding = 'utf-8') print("This is a log!", file = f) # 用print寫日志文件. |
?
3 ● 有關換行(line feed), 回車(carriage)
\r--CR(carriage return, 回車)--把光標移到所在行開頭 \n--LF(line feed, 換行)--把光標移到下一行開頭。 \r\n—CR+LF—回車+換行 |
?
4 ● 有關內置函數hash()
url = 'https://www.baidu.com/' from urllib.request import urlopen content = urlopen(url).read() dic = {hash(url):content} for key in dic: ????print(key) print(hash(url)) ''' 4122514777272343416 4122514777272343416 # 在運行一次, 得到: 8389699687043686450 8389699687043686450 ''' # ※在python的一次執行中, 對于相同的可hash的對象, hash()函數返回的都是相同的數字(因為對象在一個生命周期內) |
?
5 ● filter()函數和map()函數
# filter()函數, 類似于列表推導式 def func1(n): ???? if n % 2 == 0: ???????? return True li = [3,2,6,9,8] ? print(filter(func1, li)) # filter()方法返回一個迭代器 print(list(filter(func1, li))) ? ''' <filter object at 0x00000000025E99B0> [2, 6, 8] ''' |
def func2(n): ???? if n % 2 == 0: ???????? return n li2 = [3,2,6,9,8] ? print(list(map(func2, li2))) ''' [None, 2, 6, None, 8] ''' ? ? def func3(n): ???? return n**2 ? print(list(map(func3, li2))) ''' [9, 4, 36, 81, 64] ''' |
典型案例: 刪除 None 或者空字符串 |
# def deter(m): #???? if m and len(m.strip()) > 0: # 注意m必須寫在len(m.strip()) > 0之前, 否則會被警示None無法計算長度 #???????? return True ? # 等價于: def deter(m): ????return m and len(m.strip()) ? lst = ['test', None, '', 'str', ' ', 'END'] ? print(list(filter(deter, lst))) ''' ['test', 'str', 'END'] |
?
6 ● 內置函數__import__()
__import__('a')????????# 導入 a.py 模塊 |
轉載于:https://www.cnblogs.com/ArrozZhu/p/8393713.html
總結
以上是生活随笔為你收集整理的十五. Python基础(15)--内置函数-1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA生产环境验证_Java生产环境下
- 下一篇: python 欧姆龙plc通信_二进制P