Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Advantages to using "type" keyword in python type aliases

$
0
0

Prior to python 3.12, my understanding was that the line mta = int created a type alias named mta, but with 3.12, this line could (should?) be written as type mta = int, even if the prior syntax is still supported. These aren't interchangeable, though. For example, isinstance will accept the first as a legal second argument, but will fail with the second. (It seems that in the second case, isinstance(3, mta.__value__) works, but this seems awkward.)

I read this documentation to mean that the syntaxes are both supported to allow for backward compatibility, not that they are functionally different. Given this behavior, when is it advantageous to use the type keyword? Are there cases in which the simple (pre-3.12) assignment are inferior?

For example:

>>> mta = int>>> isinstance(3, mta)True>>> type mta = int>>> isinstance(3, mta)...TypeError: isinstance arg 2 must be a type, a tuple of types, or a union

Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>