AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁(yè) > 科技  > 軟件

趕緊試試 Python 3.12 吧,真的好用

來(lái)源: 責(zé)編: 時(shí)間:2024-06-19 15:36:45 104觀看
導(dǎo)讀Python 3.12 引入了一些新的特性和改進(jìn),提升了開(kāi)發(fā)體驗(yàn)和代碼性能。以下是其中一些值得注意的新函數(shù)和改進(jìn):1. str.removeprefix() and str.removesuffix()雖然這些函數(shù)在 Python 3.9 就已引入,但它們?cè)?Python 3.12 中

Python 3.12 引入了一些新的特性和改進(jìn),提升了開(kāi)發(fā)體驗(yàn)和代碼性能。以下是其中一些值得注意的新函數(shù)和改進(jìn):pRx28資訊網(wǎng)——每日最新資訊28at.com

pRx28資訊網(wǎng)——每日最新資訊28at.com

1. str.removeprefix() and str.removesuffix()

雖然這些函數(shù)在 Python 3.9 就已引入,但它們?cè)?Python 3.12 中變得更加廣泛使用。pRx28資訊網(wǎng)——每日最新資訊28at.com

  • **str.removeprefix(prefix)**:如果字符串以指定的前綴開(kāi)頭,則返回去掉該前綴的字符串。
  • **str.removesuffix(suffix)**:如果字符串以指定的后綴結(jié)尾,則返回去掉該后綴的字符串。
s = "HelloWorld"print(s.removeprefix("Hello"))  # 輸出: Worldprint(s.removesuffix("World"))  # 輸出: Hello

2. math.nextafter(x, y)

返回從 x 開(kāi)始,到 y 方向的下一個(gè)浮點(diǎn)數(shù)。這個(gè)函數(shù)對(duì)需要精確控制浮點(diǎn)數(shù)計(jì)算的場(chǎng)景非常有用。pRx28資訊網(wǎng)——每日最新資訊28at.com

import mathprint(math.nextafter(1.0, 2.0))  # 輸出: 1.0000000000000002print(math.nextafter(1.0, 0.0))  # 輸出: 0.9999999999999999

3. sys.orig_argv

這個(gè)屬性允許你訪問(wèn)原始的命令行參數(shù)列表,包括解釋器自身的參數(shù),而不僅僅是腳本和傳遞給腳本的參數(shù)。pRx28資訊網(wǎng)——每日最新資訊28at.com

import sysprint(sys.orig_argv)

4. functools.cache_clear()

在 Python 3.12 中,functools.cache_clear() 方法被添加到 functools.lru_cache 修飾器中,用于清除緩存。pRx28資訊網(wǎng)——每日最新資訊28at.com

from functools import lru_cache@lru_cache(maxsize=32)def fibonacci(n):    if n < 2:        return n    return fibonacci(n-1) + fibonacci(n-2)# 清除緩存fibonacci.cache_clear()

5. 新的 typing 模塊改進(jìn)

Python 3.12 對(duì) typing 模塊進(jìn)行了多項(xiàng)改進(jìn),包括更好的類(lèi)型推斷和新的類(lèi)型提示功能。例如,可以使用 Self 類(lèi)型提示方法的返回類(lèi)型為類(lèi)實(shí)例本身。pRx28資訊網(wǎng)——每日最新資訊28at.com

from typing import Selfclass MyClass:    def my_method(self) -> Self:        return self

6. contextlib.aclosing

類(lèi)似于 contextlib.closing 但用于異步生成器對(duì)象。pRx28資訊網(wǎng)——每日最新資訊28at.com

import contextlibclass AsyncGenerator:    async def __aenter__(self):        print("Entering")        return self    async def __aexit__(self, exc_type, exc, tb):        print("Exiting")    async def __aiter__(self):        for i in range(5):            yield iasync def main():    async with contextlib.aclosing(AsyncGenerator()) as agen:        async for item in agen:            print(item)# 運(yùn)行異步主函數(shù)import asyncioasyncio.run(main())

7. itertools.pairwise()

產(chǎn)生一對(duì)連續(xù)元素的迭代器。pRx28資訊網(wǎng)——每日最新資訊28at.com

import itertoolsfor pair in itertools.pairwise([1, 2, 3, 4]):    print(pair)# 輸出: (1, 2), (2, 3), (3, 4)

8. zoneinfo 模塊改進(jìn)

對(duì)時(shí)區(qū)信息進(jìn)行了增強(qiáng),更好地支持時(shí)間相關(guān)操作。pRx28資訊網(wǎng)——每日最新資訊28at.com

from zoneinfo import ZoneInfofrom datetime import datetimedt = datetime(2024, 6, 14, tzinfo=ZoneInfo("America/New_York"))print(dt)

這些新特性和改進(jìn)使得 Python 3.12 更加強(qiáng)大和易用,為開(kāi)發(fā)者提供了更多工具來(lái)編寫(xiě)高效、可維護(hù)的代碼。建議大家盡早升級(jí)并嘗試這些新特性。pRx28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-94851-0.html趕緊試試 Python 3.12 吧,真的好用

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 消滅代碼中的 if :請(qǐng)求參數(shù)校驗(yàn)的優(yōu)雅之道

下一篇: 十分鐘了解 Golang 泛型

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top