自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你使用過 Python 3.6 中針對文件系統(tǒng)的這個神奇方法嗎?

開發(fā) 后端
Python 3.6 首次發(fā)布于 2016 年,盡管它已經(jīng)發(fā)布了一段時間,但它引入的許多特性都沒有得到充分利用,而且相當(dāng)酷。下面是其中的三個。

Python 3.6 首次發(fā)布于 2016 年,盡管它已經(jīng)發(fā)布了一段時間,但它引入的許多特性都沒有得到充分利用,而且相當(dāng)酷。下面是其中的三個。

[[403559]]

分隔數(shù)字常數(shù)

快回答哪個更大,10000000 還是 200000?你在看代碼時能正確回答嗎?根據(jù)當(dāng)?shù)氐牧?xí)慣,在寫作中,你會用 10,000,000 或 10.000.000 來表示第一個數(shù)字。問題是,Python 使用逗號和句號是用于其他地方。

幸運的是,從 Python 3.6 開始,你可以使用下劃線來分隔數(shù)字。這在代碼中和使用字符串的 int() 轉(zhuǎn)換器時都可以使用:

  1. import math 
  2. math.log(10_000_000) / math.log(10) 
  1. 7.0 
  1. math.log(int("10_000_000")) / math.log(10) 
  1. 7.0 

Tau 是對的

45 度角用弧度表示是多少?一個正確的答案是 π/4,但這有點難記。記住 45 度角是一個八分之一的轉(zhuǎn)角要容易得多。正如 Tau Manifesto 所解釋的,2π,稱為 Τ,是一個更自然的常數(shù)。

在 Python 3.6 及以后的版本中,你的數(shù)學(xué)代碼可以使用更直觀的常數(shù):

  1. print("Tan of an eighth turn should be 1, got", round(math.tan(math.tau/8), 2)) 
  2. print("Cos of an sixth turn should be 1/2, got", round(math.cos(math.tau/6), 2)) 
  3. print("Sin of a quarter turn should be 1, go", round(math.sin(math.tau/4), 2)) 
  1. Tan of an eighth turn should be 1, got 1.0 
  2. Cos of an sixth turn should be 1/2, got 0.5 
  3. Sin of a quarter turn should be 1, go 1.0 

os.fspath

從 Python 3.6 開始,有一個神奇的方法表示“轉(zhuǎn)換為文件系統(tǒng)路徑”。當(dāng)給定一個 str 或 bytes 時,它返回輸入。

對于所有類型的對象,它尋找 __fspath__ 方法并調(diào)用它。這允許傳遞的對象是“帶有元數(shù)據(jù)的文件名”。

像 open() 或 stat 這樣的普通函數(shù)仍然能夠使用它們,只要 __fspath__ 返回正確的東西。

例如,這里有一個函數(shù)將一些數(shù)據(jù)寫入一個文件,然后檢查其大小。它還將文件名記錄到標(biāo)準(zhǔn)輸出,以便追蹤:

  1. def write_and_test(filename): 
  2.     print("writing into", filename) 
  3.     with open(filename, "w") as fpout: 
  4.         fpout.write("hello") 
  5.     print("size of", filename, "is", os.path.getsize(filename)) 

你可以用你期望的方式來調(diào)用它,用一個字符串作為文件名:

  1. write_and_test("plain.txt") 
  1. writing into plain.txt 
  2. size of plain.txt is 5 

然而,可以定義一個新的類,為文件名的字符串表示法添加信息。這樣可以使日志記錄更加詳細(xì),而不改變原來的功能:

  1. class DocumentedFileName: 
  2.     def __init__(self, fname, why): 
  3.         self.fname = fname 
  4.         self.why = why 
  5.     def __fspath__(self): 
  6.         return self.fname 
  7.     def __repr__(self): 
  8.         return f"DocumentedFileName(fname={self.fname!r}, why={self.why!r})" 

用 DocumentedFileName 實例作為輸入運行該函數(shù),允許 open 和 os.getsize 函數(shù)繼續(xù)工作,同時增強日志:

  1. write_and_test(DocumentedFileName("documented.txt", "because it's fun")) 
  1. writing into DocumentedFileName(fname='documented.txt'why="because it's fun"
  2. size of DocumentedFileName(fname='documented.txt'why="because it's fun") is 5 

歡迎來到 2016 年

Python 3.6 是在五年前發(fā)布的,但是在這個版本中首次出現(xiàn)的一些特性非??幔覜]有得到充分利用。如果你還沒使用,那么將他們添加到你的工具箱中。

 

責(zé)任編輯:趙寧寧 來源: Linux中國
相關(guān)推薦

2020-05-06 09:04:09

Python文件系統(tǒng)操作系統(tǒng)

2020-09-21 14:55:15

數(shù)據(jù)庫SQL技術(shù)

2022-05-05 07:25:03

Supervisor監(jiān)控Python

2024-11-07 12:33:47

2011-01-25 10:27:12

Linux 文件系統(tǒng)屬

2020-04-29 13:30:38

腳本Chrome黑科技

2021-10-08 21:00:52

數(shù)據(jù)弱引用對象

2017-03-13 13:55:53

Linux文件系統(tǒng)方法

2020-03-23 21:10:03

BashLinux文件系統(tǒng)

2023-09-13 09:20:00

日志配置Spring

2021-09-05 18:25:57

文件系統(tǒng)

2010-04-30 11:22:23

Unix系統(tǒng)

2019-03-04 14:40:46

Linux文件系統(tǒng)修復(fù)

2024-03-21 10:39:24

CIOAI

2023-11-22 07:42:01

2017-10-12 13:43:27

LinuxUbuntuZFS文件系統(tǒng)

2017-03-30 10:13:11

Linux內(nèi)核文件系統(tǒng)

2021-03-20 07:20:49

Windows10操作系統(tǒng)微軟

2021-01-01 14:36:03

Python開發(fā)語言

2021-05-07 13:39:20

Python工具代碼
點贊
收藏

51CTO技術(shù)棧公眾號