Tso tawm Python 3.10 programming lus

Tom qab ib xyoos ntawm kev txhim kho, qhov tseem ceeb tshaj tawm ntawm Python 3.10 programming lus tau nthuav tawm. Cov ceg tshiab yuav tau txais kev txhawb nqa rau ib xyoos thiab ib nrab xyoo, tom qab ntawd rau lwm peb xyoos thiab ib nrab xyoo, kev kho yuav raug tsim rau nws txhawm rau tshem tawm qhov tsis zoo.

Nyob rau tib lub sijhawm, kev sim alpha ntawm Python 3.11 ceg pib (raws li lub sijhawm tsim kho tshiab, kev ua haujlwm ntawm ceg tshiab pib tsib lub hlis ua ntej tso tawm ntawm ceg dhau los thiab mus txog qib alpha xeem los ntawm lub sijhawm tso tawm tom ntej. ). Python 3.11 ceg yuav nyob rau hauv alpha tso rau xya lub hlis, thaum lub sij hawm uas cov yam ntxwv tshiab yuav raug ntxiv thiab kab laum. Tom qab no, beta versions yuav raug sim rau peb lub hlis, thaum lub sijhawm ntxiv cov yam ntxwv tshiab yuav raug txwv thiab tag nrho cov xim yuav raug them rau kho cov kab. Rau ob lub hlis dhau los ua ntej qhov kev tso tawm, ceg yuav nyob ntawm qhov kev tso tawm tus neeg sib tw, thaum kawg yuav ua kom ruaj khov.

Tshiab ntxiv rau Python 3.10 suav nrog:

  • Siv "match" thiab "cov ntaub ntawv" tus neeg teb xov tooj rau cov qauv sib piv, uas txhim kho cov lej nyeem tau yooj yim, ua kom yooj yim sib piv ntawm cov khoom siv Python, thiab ua kom cov lej muaj kev ntseeg siab los ntawm kev tshuaj xyuas zoo li qub. Qhov kev siv ntau yam zoo li "match" tus neeg teb xov tooj muab rau hauv Scala, Rust, thiab F#, uas sib piv cov txiaj ntsig ntawm cov lus qhia tshwj xeeb nrog cov npe ntawm cov qauv uas tau teev tseg hauv cov blocks raws li "cov ntaub ntawv" tus neeg teb xov tooj.

    def http_error(status): match status: case 400: xa rov qab "Kev thov phem" rooj plaub 401|403|404: rov qab "Tsis pub" rooj plaub 418: rov qab "Kuv yog ib lub tais" rooj plaub _: xa rov qab "Ib yam dab tsi"

    Koj tuaj yeem tshem tawm cov khoom, tuples, cov npe, thiab cov kab ke sib txawv los khi cov khoom sib txawv raws li cov txiaj ntsig uas twb muaj lawm. Nws raug tso cai los txhais cov qauv nested, siv ntxiv "yog" cov xwm txheej hauv cov qauv, siv lub qhov ncauj qhov ntswg ([x, y, * so]"), cov cim tseem ceeb / tus nqi (piv txwv li, {"bandwidth": b, "latency ”: l} txhawm rau rho tawm "bandwidth" thiab "latency" qhov tseem ceeb los ntawm phau ntawv txhais lus), rho tawm cov qauv (": = "tus neeg teb xov tooj), siv lub npe tsis tu ncua hauv cov qauv. Hauv cov chav kawm, nws tuaj yeem hloov kho tus cwj pwm sib piv siv "__match__()" txoj kev.

    los ntawm dataclasses import dataclass @dataclass class Point: x: int y: int def whereis(point): match point: case Point(0, 0): print("Origin") case Point(0, y): print(f" Y={y}") case Point(x, 0): print(f"X={x}") case Point(): print("Somewhere else") case _: print("Tsis yog point") match point: case Point(x, y) if x == y: print(f"Y=X at {x}") case Point(x, y): print(f"Not on the diagonal") RED, GREEN, BLUE = 0, 1, 2 match xim: case RED: print("Kuv pom liab!") case GREEN: print("Grass is green") case BLUE: print("I'm feeling the blues :(")

  • Tam sim no nws muaj peev xwm siv cov kab lus nyob rau hauv cov lus qhia los faib cov ntsiab lus ntawm kev sau cov ntsiab lus tswj hwm thoob plaws ntau kab. Nws tseem raug tso cai tawm hauv lub comma tom qab tus thawj tswj hwm cov ntsiab lus kawg hauv pawg: nrog ( CtxManager1() as example1, CtxManager2() as example2, CtxManager3() as example3, ): ...
  • Kev txhim kho kev tshaj tawm ntawm qhov chaws qhov chaw ntawm qhov tsis raug cuam tshuam nrog cov hlua khi tsis tau kaw thiab cov lus hais hauv kab ntawv. Piv txwv li, thaum muaj ib qho tsis kaw, tsis txhob tshaj tawm qhov yuam kev syntax hauv cov qauv hauv qab no, tus pointer tam sim no qhia txog qhov qhib brace thiab qhia tias tsis muaj qhov kaw. Cov ntaub ntawv "example.py", kab 1 expect = {9:1, 18:2, 19:2, 27:3, 28:3, 29:3, 36:4, 37:4, ^SyntaxError: '{' yeej tsis raug kaw

    Ntxiv cov lus tshwj xeeb tshwj xeeb syntax yuam kev: ploj lawm ":" cim ua ntej ib qho thaiv thiab hauv phau ntawv txhais lus, tsis sib cais ib tuple nrog cov kab lus, ploj lawm ib qho comma hauv cov npe, qhia qhov "sim" thaiv yam tsis muaj "tsuas yog" thiab "thaum kawg", siv "= " hloov ntawm " = =" hauv kev sib piv, qhia *-qhia hauv f-strings. Tsis tas li ntawd, nws ua kom ntseeg tau tias tag nrho cov teeb meem qhia tau hais txog, tsis yog qhov pib xwb, thiab cov ntaub ntawv qhia meej ntxiv txog cov ntsiab lus ntawm qhov tsis raug cuam tshuam nrog kev tsis raug indentation. >>> def foo(): … if lel: … x = 2 File β€œβ€, kab 3 x = 2 ^ IndentationError: xav tau ib qho thaiv thaiv tom qab 'yog' nqe lus hauv kab 2

    Nyob rau hauv qhov yuam kev tshwm sim los ntawm typos hauv cov npe ntawm cov cwj pwm thiab cov npe sib txawv hauv cov haujlwm, kev pom zoo nrog lub npe raug yog qhov tso tawm. >>> collections.namedtoplo Traceback (tam sim no hu xov tooj kawg): Cov ntaub ntawv "", kab 1, hauv AttributeError: module 'collections' tsis muaj attribute 'namedtoplo'. Koj puas tau txhais: npetuple?

  • Rau cov cuab yeej debugging thiab profilers, cov xwm txheej taug qab tau muab nrog cov lej ntawm cov lej raug tua.
  • Ntxiv sys.flags.warn_default_encoding teeb tsa los tso saib cov lus ceeb toom txog qhov yuam kev cuam tshuam nrog TextIOWrapper thiab qhib () ua UTF-8 encoded cov ntaub ntawv yam tsis tau qhia meej meej txog 'encoding =Β»utf-8β€³' kev xaiv (ASCII encoding yog siv los ntawm lub neej ntawd). Qhov kev tso tawm tshiab kuj tseem muab lub peev xwm los qhia qhov 'encoding = "locale"' tus nqi los teeb tsa qhov encoding raws li thaj chaw tam sim no.
  • Tus neeg teb xov tooj tshiab tau ntxiv rau qhov kev ntaus ntawv, uas muab cov cuab yeej rau kev qhia meej hom annotations, tso cai rau siv cov syntax β€œX | Y" xaiv ib hom (X hom lossis Y hom). def square(number: int | float) -> int | ntab: rov qab tus lej ** 2 yog sib npaug rau qhov kev txhawb nqa yav dhau los: def square(number: Union[int, ntab]) -> Union[int, ntab]: rov qab tus lej ** 2
  • Tus neeg teb xov tooj Concatenate thiab ParamSpec hloov pauv tau ntxiv rau hauv kev ntaus ntawv, uas tso cai rau koj hla cov ntaub ntawv ntxiv rau kev kuaj xyuas hom zoo li qub thaum siv Callable. Lub typing module kuj ntxiv qhov tseem ceeb tshwj xeeb TypeGuard rau annotate hom kev tiv thaiv kev ua haujlwm thiab TypeAlias ​​​​kom meej meej txhais hom alias. StrCache: TypeAlias ​​= 'Cache[str]' # hom alias
  • Lub zip() muaj nuj nqi siv ib qho kev xaiv " nruj" chij, uas, thaum teev tseg, xyuas seb cov lus sib cav uas tau hais dua yog qhov ntev. >>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True)) [('a', 1), ('b', 2) , ('c', 3)] >>> list(zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True)) Traceback (feem ntau hu ua zaum kawg ): … ValueError: zip() argument 2 is long than argument 1
  • Tshiab built-in functions aiter() thiab anext() tau npaj nrog kev siv ntawm asynchronous analogues rau cov haujlwm iter() thiab tom ntej().
  • Kev ua haujlwm ntawm str(), bytes() thiab bytearray() constructors thaum ua haujlwm nrog cov khoom me me tau nrawm los ntawm 30-40%.
  • Txo tus naj npawb ntawm ntshuam ua haujlwm nyob rau hauv lub runpy module. Cov lus txib "python3 -m module_name" tam sim no khiav ntawm nruab nrab 1.4 lub sij hawm sai dua vim qhov txo qis ntawm cov khoom siv txawv teb los ntawm 69 txog 51.
  • LOAD_ATTR cov lus qhia siv lub caching mechanism rau ib tus neeg opcodes, uas ua rau nws muaj peev xwm ua kom ceev ua hauj lwm nrog cov cwj pwm tsis tu ncua txog li 36%, thiab nrog cov slots txog li 44%.
  • Thaum tsim Python nrog qhov "--enable-optimizations" kev xaiv, "-fno-semantic-interposition" hom yog tam sim no enabled, uas tso cai rau kev ceev tus neeg txhais lus nce mus txog 30% piv rau lub tsev nrog "--enable-shared ”kev xaiv.
  • Cov hashlib thiab ssl modules tau ntxiv kev txhawb nqa rau OpenSSL 3.0.0 thiab tsis txhawb nqa OpenSSL versions laus dua 1.1.1.
  • Cov parser qub tau raug tshem tawm, uas tau hloov pauv hauv ceg dhau los los ntawm PEG (Parsing Expression Grammar) parser. Lub formatter module tau raug tshem tawm. Lub voj parameter tau raug tshem tawm ntawm asyncio API. Lub Py_UNICODE_str* ua haujlwm uas tswj Py_UNICODE* cov hlua tau raug tshem tawm.
  • Lub distutils module tau raug deprecated thiab tau teem rau kev tshem tawm hauv Python 3.12. Es tsis txhob distutils, nws raug pom zoo kom siv cov setuptools, ntim, platform, shutil, subprocess thiab sysconfig modules. Tus qauv wstr hauv PyUnicodeObject tau raug tshem tawm thiab teem caij tshem tawm.

Tau qhov twg los: opennet.ru

Ntxiv ib saib