概述
??隨著Python在機器學習和資料科學領域的應用越來越廣泛,相關的Python庫也增長的非常快,但是Python本身存在一個非常要命的問題,就是Python2和Python3,兩個版本互不兼容,而且Github上Python2的開源庫有很多不兼容Python3,導致大量的Python2的用戶不愿意遷移到Python3,
??Python3在很多方面都做出了改變,優化了Python2的很多不足,標準庫也擴充了很多內容,例如協程相關的庫,現在列舉一些Python3里提供的功能,跟你更好的從Python2遷移到Python3的理由,

這里要注意:不管你是為了Python就業還是興趣愛好,記住:專案開發經驗永遠是核心,如果你沒有2020最新python入門到高級實戰視頻教程,可以去小編的Python交流.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,里面很多新python教程專案,還可以跟老司機交流討教!
系統檔案路徑處理庫:pathlib
??使用Python2的同學,應該都用過os.path這個庫,來處理各種各樣的路徑問題,比如拼接檔案路徑的函式:os.path.join(),用Python3,你可以使用pathlib很方便的完成這個功能:
1 2 3 4 5 6 7 8 9 10 11
|
from pathlib import Path
dataset = 'wiki_images' datasets_root = Path('/path/to/datasets/')
train_path = datasets_root / dataset / 'train' test_path = datasets_root / dataset / 'test'
for image_path in train_path.iterdir(): with image_path.open() as f: # note, open is a method of Path object # do something with an image
|
相比與os.path.join()函式,pathlib更加安全、方便、可讀,pathlib還有很多其他的功能,
1 2 3 4 5 6 7
|
p.exists() p.is_dir() p.parts() p.with_name('sibling.png') # only change the name, but keep the folder p.with_suffix('.jpg') # only change the extension, but keep the folder and the name p.chmod(mode) p.rmdir()
|
型別提醒: Type hinting
??在Pycharm中,型別提醒是這個樣子的:

??型別提醒在復雜的專案中可以很好的幫助我們規避一些手誤或者型別錯誤,Python2的時候是靠IDE來識別,格式IDE識別方法不一致,并且只是識別,并不具備嚴格限定,例如有下面的代碼,引數可以是numpy.array , astropy.Table and astropy.Column, bcolz, cupy, mxnet.ndarray等等,
1 2 3 4 5 6
|
def repeat_each_entry(data): """ Each entry in the data is doubled <blah blah nobody reads the documentation till the end> """ index = numpy.repeat(numpy.arange(len(data)), 2) return data[index]
|
同樣上面的代碼,傳入pandas.Series型別的引數也是可以,但是運行時會出錯,
1
|
repeat_each_entry(pandas.Series(data=https://www.cnblogs.com/chengxuyuanaa/p/[0, 1, 2], index=[3, 4, 5])) # returns Series with Nones inside
|
??這還只是一個函式,對于大型的專案,會有好多這樣的函式,代碼很容易就跑飛了,所以確定的引數型別對于大型專案來說非常重要,而Python2沒有這樣的能力,Python3可以,
1
|
def repeat_each_entry(data: Union[numpy.ndarray, bcolz.carray]):
|
??目前,比如JetBrains家的PyCharm已經支持Type Hint語法檢查功能,如果你使用了這個IDE,可以通過IDE功能進行實作,如果你像我一樣,使用了SublimeText編輯器,那么第三方工具mypy可以幫助到你,
??PS:目前型別提醒對ndarrays/tensors支持不是很好,
運行時型別檢查:
正常情況下,函式的注釋處理理解代碼用,其他沒什么用,你可以是用enforce來強制運行時檢查型別,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@enforce.runtime_validation def foo(text: str) -> None: print(text)
foo('Hi') # ok foo(5) # fails
@enforce.runtime_validation def any2(x: List[bool]) -> bool: return any(x)
any ([False, False, True, False]) # True any2([False, False, True, False]) # True
any (['False']) # True any2(['False']) # fails
any ([False, None, "", 0]) # False any2([False, None, "", 0]) # fails
|
使用@特殊字符表示矩陣乘法
如下代碼:
1 2 3 4 5 6
|
# l2-regularized linear regression: || AX - b ||^2 + alpha * ||x||^2 -> min
# Python 2 X = np.linalg.inv(np.dot(A.T, A) + alpha * np.eye(A.shape[1])).dot(A.T.dot(b)) # Python 3 X = np.linalg.inv(A.T @ A + alpha * np.eye(A.shape[1])) @ (A.T @ b)
|
使用@符號,整個代碼變得更可讀和方便移植到其他如numpy、tensorflow等庫,
**特殊字符來遞回檔案路徑
在Python2中,遞回查找檔案不是件容易的事情,即使使用glob庫,但是python3中,可以通過通配符簡單的實作,
1 2 3 4 5 6 7 8 9 10 11 12
|
import glob
# Python 2 found_images = \ glob.glob('/path/*.jpg') \ + glob.glob('/path/*/*.jpg') \ + glob.glob('/path/*/*/*.jpg') \ + glob.glob('/path/*/*/*/*.jpg') \ + glob.glob('/path/*/*/*/*/*.jpg')
# Python 3 found_images = glob.glob('/path/**/*.jpg', recursive=True)
|
和之前提到的pathlib一起使用,效果更好:
1 2
|
# Python 3 found_images = pathlib.Path('/path/').glob('**/*.jpg')
|
Print函式
列印到指定檔案
1 2
|
print >>sys.stderr, "critical error" # Python 2 print("critical error", file=sys.stderr) # Python 3
|
不使用join函式拼接字串
1 2 3
|
# Python 3 print(*array, sep='\t') print(batch, epoch, loss, accuracy, time, sep='\t')
|
重寫print函式
1 2 3 4
|
# Python 3 _print = print # store the original print function def print(*args, **kargs): pass # do something useful, e.g. store output to some file
|
再比如下面的代碼
1 2 3 4 5 6 7 8 9 10 11
|
@contextlib.contextmanager def replace_print(): import builtins _print = print # saving old print function # or use some other function here builtins.print = lambda *args, **kwargs: _print('new printing', *args, **kwargs) yield builtins.print = _print
with replace_print(): <code here will invoke other print function>
|
雖然上面這段代碼也能達到重寫print函式的目的,但是不推薦使用,
字串格式化
python2提供的字串格式化系統還是不夠好,太冗長麻煩,通常我們會寫這樣一段代碼來輸出日志資訊:
1 2 3 4 5 6 7 8 9 10 11 12
|
# Python 2 print('{batch:3} {epoch:3} / {total_epochs:3} accuracy: {acc_mean:0.4f}±{acc_std:0.4f} time: {avg_time:3.2f}'.format( batch=batch, epoch=epoch, total_epochs=total_epochs, acc_mean=numpy.mean(accuracies), acc_std=numpy.std(accuracies), avg_time=time / len(data_batch) ))
# Python 2 (too error-prone during fast modifications, please avoid): print('{:3} {:3} / {:3} accuracy: {:0.4f}±{:0.4f} time: {:3.2f}'.format( batch, epoch, total_epochs, numpy.mean(accuracies), numpy.std(accuracies), time / len(data_batch) ))
|
輸出的結果是:
1
|
120 12 / 300 accuracy: 0.8180±0.4649 time: 56.60
|
python3.6的f-strings功能實作起來就簡單多了,
1 2
|
# Python 3.6+ print(f'{batch:3} {epoch:3} / {total_epochs:3} accuracy: {numpy.mean(accuracies):0.4f}±{numpy.std(accuracies):0.4f} time: {time / len(data_batch):3.2f}')
|
而且,在撰寫查詢或生成代碼片段時非常方便:
1
|
query = f"INSERT INTO STATION VALUES (13, '{city}', '{state}', {latitude}, {longitude})"
|
嚴格排序
下面這些比較操作在python3里是非法的
1 2 3 4 5 6 7 8
|
# All these comparisons are illegal in Python 3 3 < '3' 2 < None (3, 4) < (3, None) (4, 5) < [4, 5]
# False in both Python 2 and Python 3 (4, 5) == [4, 5]
|
不同型別的資料無法排序
1
|
sorted([2, '1', 3]) # invalid for Python 3, in Python 2 returns [2, 3, '1']
|
NLP Unicode問題
1 2 3 4 5 6 7 8 9 10 11 12 13
|
s = '您好' print(len(s)) print(s[:2])
Output:
Python 2: 6\n?? Python 3: 2\n您好.
x = u'со' x += 'co' # ok x += 'со' # fail
|
下面這段代碼在Python2里運行失敗但是Python3會成功運行,Python3的字串都是Unicode編碼,所以這樣對NLP來說很方便,再比如:
1 2
|
'a' < type < u'a' # Python 2: True 'a' < u'a' # Python 2: False
|
1 2 3 4
|
from collections import Counter Counter('M?belstück') Python 2: Counter({'\xc3': 2, 'b': 1, 'e': 1, 'c': 1, 'k': 1, 'M': 1, 'l': 1, 's': 1, 't': 1, '\xb6': 1, '\xbc': 1}) Python 3: Counter({'M': 1, '?': 1, 'b': 1, 'e': 1, 'l': 1, 's': 1, 't': 1, 'ü': 1, 'c': 1, 'k': 1})
|
字典
CPython3.6+里的dict默認的行為和orderdict很類似
1 2 3 4 5 6 7
|
import json x = {str(i):i for i in range(5)} json.loads(json.dumps(x)) # Python 2 {u'1': 1, u'0': 0, u'3': 3, u'2': 2, u'4': 4} # Python 3 {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}
|
同樣的,**kwargs字典內容的資料和傳入引數的順序是一致的,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
from torch import nn
# Python 2 model = nn.Sequential(OrderedDict([ ('conv1', nn.Conv2d(1,20,5)), ('relu1', nn.ReLU()), ('conv2', nn.Conv2d(20,64,5)), ('relu2', nn.ReLU()) ]))
# Python 3.6+, how it *can* be done, not supported right now in pytorch model = nn.Sequential( conv1=nn.Conv2d(1,20,5), relu1=nn.ReLU(), conv2=nn.Conv2d(20,64,5), relu2=nn.ReLU()) )
|
Iterable unpacking
1 2 3 4 5 6 7 8 9
|
# handy when amount of additional stored info may vary between experiments, but the same code can be used in all cases model_paramteres, optimizer_parameters, *other_params = load(checkpoint_name)
# picking two last values from a sequence *prev, next_to_last, last = values_history
# This also works with any iterables, so if you have a function that yields e.g. qualities, # below is a simple way to take only last two values from a list *prev, next_to_last, last = iter_train(args)
|
更高性能的默認pickle engine
1 2 3 4 5 6 7 8 9 10 11
|
# Python 2 import cPickle as pickle import numpy print len(pickle.dumps(numpy.random.normal(size=[1000, 1000]))) # result: 23691675
# Python 3 import pickle import numpy len(pickle.dumps(numpy.random.normal(size=[1000, 1000]))) # result: 8000162
|
縮短到Python2時間的1/3
更安全的串列推導
1 2 3 4 5
|
labels = <initial_value> predictions = [model.predict(data) for data, labels in dataset]
# labels are overwritten in Python 2 # labels are not affected by comprehension in Python 3
|
更簡易的super()
1 2 3 4 5 6 7 8 9
|
# Python 2 class MySubClass(MySuperClass): def __init__(self, name, **options): super(MySubClass, self).__init__(name='subclass', **options) # Python 3 class MySubClass(MySuperClass): def __init__(self, name, **options): super().__init__(name='subclass', **options)
|
Multiple unpacking
合并兩個Dict
1 2 3 4 5
|
x = dict(a=1, b=2) y = dict(b=3, d=4) # Python 3.5+ z = {**x, **y} # z = {'a': 1, 'b': 3, 'd': 4}, note that value for `b` is taken from the latter dict.
|
Python3.5+不僅僅合并dict很方便,合并list等也很方便
1 2 3
|
[*a, *b, *c] # list, concatenating (*a, *b, *c) # tuple, concatenating {*a, *b, *c} # set, union
|
1 2 3 4 5
|
Python 3.5+ do_something(**{**default_settings, **custom_settings})
# Also possible, this code also checks there is no intersection between keys of dictionaries do_something(**first_args, **second_args)
|
整數型別
python2提供了兩個整數型別:int和long,python3只提供有個整數型別:int,如下的代碼:
isinstance(x, numbers.Integral) # Python 2, the canonical way
isinstance(x, (long, int)) # Python 2
isinstance(x, int) # Python 3, easier to remember
總結
python3提供了很多新的特性,方便我們編碼的同時,也帶來了更好的安全性和較高的性能,而且官方也一直推薦盡快遷移到python3,當然,遷移的代價因系統而異,希望這篇文章能對你遷移python2到python3有些幫助,
最后要注意:不管你是為了Python就業還是興趣愛好,記住:專案開發經驗永遠是核心,如果你沒有2020最新python入門到高級實戰視頻教程,可以去小編的Python交流.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,里面很多新python教程專案,還可以跟老司機交流討教!
本文的文字及圖片來源于網路加上自己的想法,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/152952.html
標籤:Python
上一篇:編程語言概念
下一篇:二、Python爬蟲-urllib庫資料挖掘