11

搞爆 Python 的各種姿勢

 2 years ago
source link: https://blog.gslin.org/archives/2022/06/07/10731/%e6%90%9e%e7%88%86-python-%e7%9a%84%e5%90%84%e7%a8%ae%e5%a7%bf%e5%8b%a2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

搞爆 Python 的各種姿勢

Hacker News 首頁上看到「no-op statements syntactically valid only since Python X.Y」這個專案,搞爆各個版本 Python 的各種方式,從 Python 2.4+ 一路到 3.11+ (不過中間有少了 3.2 與 3.4)。

專案要求的條件是 no-op,所以像是 import 這種行為都會產生 side effect,所以就不能用 sys.version_info 這個變數了:

This is a collection of no-op statements that are syntactically valid only since Python X.Y, for most X.Y ≥ 2.4.

看了一下裡面的例子,反而看到一些有趣的東西,像是原來這種語法在 Python 2.3 是不能跑的:

(0 for x in [])  # Python >= 2.4 is required

然後 0_0 這種方便表示數字的寫法在 Python 3.6+ 才能動:

0_0  # Python >= 3.6 is required

有些東西真的是用習慣就忘記了,遇到一些古董環境可能會中獎然後在那邊疑惑半天 XD

看起來這個專案應該比較偏娛樂性質?實際應用上有很多其他比較常見的方式檢查環境才對 XD 但馬上想到,在打黑箱的時候可以用這個方法判斷 Python 的環境版本?

Related

一些不太容易注意到的 Python 安全性問題

前幾天看到「10 Unknown Security Pitfalls for Python」這篇,講 Python ecosystem 裡面的一些設計造成的安全性問題,裡面很多東西都很有趣,而且有些算是共通的,其他程式語言也會中獎... 第一個是開發者用 assert() 確認權限,但 assert() 的設計是 debug 用的功能,所以可以預期在 optimization 後會被拿掉 (其他程式語言也有類似的功能),而導致權限沒被檢查而產生 security incident。 第二個是 os.makedirs() 所指定的 mode,依照說明看起來是 Python 改變行為了,變成只有最終的那個目錄照著設定: In Python < 3.6, the folders A, B and C are each created with permission 700. However, in Python > 3.6, only the…

February 3, 2022

In "Computer"

Python 裡使用超過 Double Precision 的運算

Hacker News 上爬到的,是一篇 2019 的文章:「When Double Precision Is Not Enough (adambaskerville.github.io)」,原文在「T>T: When Double Precision is Not Enough」。 作者是拿矩陣 (matrix) 的運算當例子,遇到了 double precision 造成的計算誤差問題: There is no error with the program; this discrepancy is caused by a loss of numerical accuracy in the eigenvalue calculation due to the limitation of hardware…

March 20, 2022

In "Computer"

畫 Python 下記憶體使用情況的 Flamegraph:Memray

前幾天的 Hacker News Daily 上看到的東西,是由 Bloomberg 開發出來的工具 Memray,這個工具是一個 Python 套件: Memray is a memory profiler for Python. It can track memory allocations in Python code, in native extension modules, and in the Python interpreter itself. 套件有多種輸出,其中一種是可以產生出記憶體使用情況的 flamegraph,轉成圖檔後像是這樣: 官方支援 Python 3.7+: Memray requires Python 3.7+ and can be easily installed using…

April 25, 2022

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on June 7, 2022Categories Computer, Joke, Murmuring, Programming, RecreationTags error, no, op, programming, python, syntax, valid

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

Post navigation


Recommend

  • 11
    • blog.csdn.net 4 years ago
    • Cache

    純前端 - 各種實現進度條

    進度條是一個非常常見的功能,實現起來也不難,一般我們會用 div 來實現。作為一個這麼常見的需求,這篇就要來康康有哪些,以純前端有哪些有意思的方式來實現進度條。 基礎版 - div ps. (這邊以下 gif 周圍稍有些瑕疵,我也不知...

  • 7

    從調校 HTTP Server 的文章中學各種奇技淫巧 在「Extreme HTTP Performance Tuning: 1.2M API req/s on a 4 vCPU EC2 Instance」這篇文章裡面,...

  • 17
    • blog.darkthread.net 3 years ago
    • Cache

    PowerShell Script 結束執行的正確姿勢

    PowerShell Script 結束執行的正確姿勢-黑暗執行緒在 PowerShell 中想提前結束程式有好幾種寫法,例如 return、exit,之前沒認真比過差異,胡亂用踩了坑,特整理筆記備忘。 先用以下 Test-ExitScript.ps1 程式示範,依傳入參數模擬四種提前結束 Script...

  • 16
    • blog.darkthread.net 3 years ago
    • Cache

    從前端整合 WebForm 的各種方法

    從前端整合 WebForm 的各種方法 2021-07-16 05:06 PM 0 841 即便前端發展已十分成熟,幾乎無所不能,但仍有些必須依賴 ASP.NET WebForm 的場合 - 例如,在前端...

  • 3
    • www.hwchiu.com 3 years ago
    • Cache

    Infrastructure 各種踩雷經驗

    Infrastructure 各種踩雷經驗 Posted on 2021-12-13 Views: 40 閱讀筆記-踩雷分享 ref: https://matduggan.com/mistakes/ 本文是作者...

  • 5

    閱讀筆記: 「新手閱讀,我踩過的 Terraform 各種雷」 Posted on 2022-05-06 Views: 10 「新手閱讀,我踩過的 Terraform 各種雷」 標題: 「新手閱讀,我踩過的 Terraform 各種雷」

  • 5
    • dannypsnl.github.io 2 years ago
    • Cache

    NOTE: 演算法的各種時間複雜度

    NOTE: 演算法的各種時間複雜度演算法時間複雜度分析:對每個不同的輸入大小,其基本運算所執行的次數 所有情況時間複雜度 T(n)T(n)

  • 9

    翻一下 Linux container 的各種 overhead 想要查一下 Linux 下跑 container 的 overhead,發現大...

  • 10

    分析 Windows 磁碟空間不足的正確姿勢 2022-07-24 09:17 PM 0 ...

  • 10

    依背景色決定文字顏色的正確姿勢 - W3C 標準-黑暗執行緒 再來聊聊「依據背景色切換黑字或白字,確保文字明顯容易閱讀」這檔事。 過去我都用 Github Copilot 教我的

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK