Dab tsi yog hom casting hauv Python?
Dab tsi yog hom casting hauv Python?

Video: Dab tsi yog hom casting hauv Python?

Video: Dab tsi yog hom casting hauv Python?
Video: Installing Python in Windows 10! 2024, Plaub Hlis Ntuj
Anonim

Casting yog thaum koj hloov pauv tus nqi sib txawv hom rau lwm tus. Qhov no yog, hauv Python , donewithfunctions xws li int() or float() or str(). Ib qho kev sib txawv heev uas koj hloov tus lej, tam sim no raws li txoj hlua rau hauv tus lej.

Hauv qhov no, hom casting nrog piv txwv yog dab tsi?

Ib piv txwv ntawm ntaus ntawv yog convertinganinteger rau ib txoj hlua. Txhawm rau hloov mus rau tus nqi ze tshaj plaws, ntxiv 0.5 rau tus lej taw tes ntab thiab tom qab ntawd ntaus ntawv nws toan integer yuav ua kom tau qhov tseeb. Rau piv txwv , hauv kev ua haujlwm hauv qab no, ob qho tib si 2.75 thiab 3.25 yuav tau txais mus rau 3.

Tsis tas li ntawd, hom kev quab yuam hauv Python yog dab tsi? Kev quab yuam yog qhov implicit hloov dua siab tshiab ofaninstance ib hom rau lwm tus thaum lub sijhawm ua haujlwm uas muaj ob qhov kev sib cav ntawm tib yam hom . Siv cov quab yuam muaj nuj nqi yog nyob rau hauv tsis tshua tsim nyog txij li thaum Python cov tus txhais lus yeej normalizes valuesinarithmetic expressions.

Ib sab saum toj, dab tsi yog hom () hauv Python?

Python | hom() muaj nuj nqi hom() txoj kev rov qab chav kawm hom oftheargument(object) dhau li parameter. Yog ib qho kev sib cav hom (obj) dhau lawm, nws rov qab los hom ofgivenobject. Yog peb sib cav hom (npe, bases, dict) ispassed, nws rov los tshiab hom khoom.

Dab tsi yog hom casting ntawm qhov sib txawv?

Hom casting hais txog kev hloov ib qho sib txawv ntawm ib cov ntaub ntawv hom rau hauv lwm tus. Piv txwv li, yog tias koj muab tus nqi aninteger rau qhov taw tes ntab sib txawv , lub compiler yuav hloov lub int mus rau ib tug ntab. Casting tso cai rau koj ua qhov no hom hloov dua siab tshiab, los yog yuam nws thaum nws yuav tsis tshwm sim.

Pom zoo: