Gabatarwa zuwa Ƙarfin Rubutu a Python
A duniyar shirye-shirye, bugu mai ƙarfi yana nufin yadda tsananin yaren shirye-shirye ke tilasta dokoki don sanya ƙima ga masu canji, ayyuka, da hanyoyi. A cikin yaren da aka buga mai ƙarfi, dole ne a fayyace nau'ikan masu canjin bayanai a sarari, kuma harshen yana ba da damar iyakance, idan akwai, jujjuyawar fakice tsakanin nau'ikan bayanai daban-daban. Python Harshe ne da aka buga a hankali, ma'ana ana sanya masu canji nau'i a lokacin aiki. Python, duk da haka, ana kuma la'akari da yare mai ƙarfi mai ƙarfi saboda yana ɗaukar tsauraran matakan duba nau'in lokacin aiki. Wannan labarin zai tattauna mahimmancin rubutu mai ƙarfi a cikin Python da yadda ake aiwatar da bugu mai ƙarfi ta amfani da dabaru, dakunan karatu, da ayyuka daban-daban. Za mu yi tafiya ta cikin wasu misalan lambobin don haskaka ra'ayoyi da mafita, sannan da cikakken bayani.
Bayanin Nau'ukan a Python tare da Nau'in Alamu
Python ya gabatar rubuta alamu tare da PEP 484, wanda ke ba masu haɓaka damar yin bayanin ayyuka da hanyoyin tare da nau'ikan bayanan da ake tsammani don sigogin shigarwa da ƙimar dawowa. Wannan haɓakawa ya sauƙaƙe wa masu haɓakawa don rubutawa da fahimtar lamba, haɓaka iya karanta lambar, da taimaka kayan aikin tabbatar da daidaiton lambar yayin haɓakawa.
Don kwatanta yadda ake amfani da nau'in alamu, za mu rubuta aikin da ke ƙididdige yanki na rectangle:
from typing import Tuple def calculate_area(dimensions: Tuple[int, int]) -> int: width, height = dimensions return width * height my_dimensions = (10, 20) area = calculate_area(my_dimensions) print(area)
A cikin misalin da ke sama, aikin calculate_area
yana karɓar tuple na lamba biyu azaman shigarwa (dimensions: Tuple[int, int])
) kuma ya dawo da ƙimar lamba (-> int
). Nau'in alamu suna ba da cikakkiyar fahimtar shigarwar da ake tsammani da fitarwa, yana sauƙaƙa ga sauran masu haɓakawa don fahimta da amfani da aikin.
Ƙarfafa Buga mai ƙarfi tare da na'ura mai gadi
Duk da yake rubuta alamun suna inganta iya karanta lambar, ba sa tilasta yin rubutu mai ƙarfi a lokacin aiki. Don wannan dalili, zamu iya amfani da ɗakin karatu da ake kira nau'in tsaro, wanda ke ba da kayan ado don tilasta yin duba nau'in bisa ga nau'in alamu a lokacin aiki. Don nuna nau'in nau'in nau'i, za mu canza misalin da ya gabata:
from typing import Tuple from typeguard import typechecked @typechecked def calculate_area(dimensions: Tuple[int, int]) -> int: width, height = dimensions return width * height my_dimensions = (10, 20) area = calculate_area(my_dimensions) print(area) my_wrong_dimensions = (10, "20") area = calculate_area(my_wrong_dimensions) # This will raise a TypeError
Ta hanyar ƙawata aikin tare da @typechecked
, da nau'in tsaro ɗakin karatu yana bincika shigarwar da fitarwar aikin akan alamun da aka bayar. Idan aka gano rashin daidaito, a TypeError
an tashe shi. Wannan yana taimakawa kama yuwuwar kwari yayin haɓakawa.
Amfani da Nau'in Dubawa tare da Pyright
Wata hanyar da za a iya aiwatar da bugu mai ƙarfi ita ce amfani da madaidaicin nau'in abin dubawa kamar Pyright. Pyright shine mai duba nau'in sauri don Python wanda ke mai da hankali kan aiki, yana nuna keta a lokacin haɓakawa ba tare da shafar lokacin aiki ba. Koyaya, ba kamar mafita na baya ba, yana buƙatar haɗawa cikin yanayin haɓaka ku, kamar plugin ɗin edita.
A ƙarshe, bugawa mai ƙarfi yana taka muhimmiyar rawa wajen haɓaka amincin lambar, kiyayewa, da iya karantawa. Python yana goyan bayan bugu mai ƙarfi ta nau'in alamu da dakunan karatu kamar nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau’in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau'in nau’in nau'in nau'in nau'in nau'in nau’in nau'in nau'in nau'in nau'in nau'in nau'in nau’in nau'in nau'in nau'in nau'i) yana tallafawa nau’in labura kamar nau’in nau’in rubutu da kuma Pyright, wanda ke taimaka wa masu haɓakawa aiwatar da nau’in cak da kama kurakurai masu yuwuwa a farkon tsarin ci gaba. Yin amfani da waɗannan kayan aikin da dabaru, masu haɓakawa na iya rubuta ƙaƙƙarfan ƙaƙƙarfan aikace-aikacen Python marasa kuskure.