Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / Python

typing.NewType

Stvara zaseban statički podtip, primjerice za identifikator temeljen na int.

Verzija: Python 3.14; repository image, production runtime not attested

Sintaksa

UserId = NewType("UserId", int)

Stvara zaseban statički podtip, primjerice za identifikator temeljen na int.

Identifikator vrijedi gdje se traži int, ali obrnuta dodjela nije automatska.

Pri izvršavanju poziv bez provjere vraća isti objekt, a zbroj ostaje običan int.

Primjeri

from typing import NewType
UserId = NewType("UserId", int)
raw = int("1200")
value = UserId(raw)
print(value is raw, type(value + 1).__name__)
print(UserId("x"), UserId.__supertype__ is int)

Rezultat

True int
x True

Pri izvršavanju poziv bez provjere vraća isti objekt, a zbroj ostaje običan int.

Izvori