偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

[[403559]]

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

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

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

  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 是對(duì)的

45 度角用弧度表示是多少?一個(gè)正確的答案是 π/4,但這有點(diǎn)難記。記住 45 度角是一個(gè)八分之一的轉(zhuǎn)角要容易得多。正如 Tau Manifesto 所解釋的,2π,稱為 Τ,是一個(gè)更自然的常數(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 開(kāi)始,有一個(gè)神奇的方法表示“轉(zhuǎn)換為文件系統(tǒng)路徑”。當(dāng)給定一個(gè) str 或 bytes 時(shí),它返回輸入。

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

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

例如,這里有一個(gè)函數(shù)將一些數(shù)據(jù)寫入一個(gè)文件,然后檢查其大小。它還將文件名記錄到標(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)) 

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

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

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

  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í)例作為輸入運(yùn)行該函數(shù),允許 open 和 os.getsize 函數(shù)繼續(xù)工作,同時(shí)增強(qiáng)日志:

  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 

歡迎來(lái)到 2016 年

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

 

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

2020-05-06 09:04:09

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

2020-09-21 14:55:15

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

2022-05-05 07:25:03

Supervisor監(jiān)控Python

2024-11-07 12:33:47

2020-04-29 13:30:38

腳本Chrome黑科技

2021-10-08 21:00:52

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

2011-01-25 10:27:12

Linux 文件系統(tǒng)屬

2023-09-13 09:20:00

日志配置Spring

2020-03-23 21:10:03

BashLinux文件系統(tǒng)

2021-09-05 18:25:57

文件系統(tǒng)

2017-03-13 13:55:53

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

2024-03-21 10:39:24

CIOAI

2023-11-22 07:42:01

2010-04-30 11:22:23

Unix系統(tǒng)

2019-03-04 14:40:46

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

2021-01-01 14:36:03

Python開(kāi)發(fā)語(yǔ)言

2010-03-04 14:44:18

2021-03-20 07:20:49

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

2025-04-15 08:00:00

Java開(kāi)發(fā)服務(wù)網(wǎng)格

2021-05-07 13:39:20

Python工具代碼
點(diǎn)贊
收藏

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