When I want to typehint a tuple in Python like:
def func(var: tuple[int, int]): # do something func((1, 2)) # would be fine func((1, 2, 3)) # would throw an error
It is required to give the exact number of items in the tuple. That's different from list typehinting:
def func(var: list[int]): # do something func([1]) # would be fine func([1, 2]) # would also be fine func([1, 2, 3]) # would also be fine
That's consequentially, in a way, because of the type of tuples. Because they are designed not to be changed, you have to hardcode the amount of items in it.
So my question is, is there a way to make the number of items in a tuple type hint flexible? I tried something like that but it didn't work:
def func(var: tuple[*int]):
Introduction to Python type hints It means that you need to declare types of variables, parameters, and return values of a function upfront. The predefined types allow the compilers to check the code before compiling and running the program.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.
To specify a variable-length tuple of homogeneous type, use literal ellipsis, e.g. Tuple[int, ...] . A plain Tuple is equivalent to Tuple[Any, ...] , and in turn to tuple .
typing. Tuple is special here in that it lets you specify a specific number of elements expected and the type of each position. Use ellipsis if the length is not set and the type should be repeated: Tuple[float, ...] describes a variable-length tuple with float s. For typing.
Tuple type; Tuple[X, Y] is the type of a tuple of two items with the first item of type X and the second of type Y. Example: Tuple[T1, T2] is a tuple of two elements corresponding to type variables T1 and T2.
To set type hints for multiple types, you can use Union from the typing module. Second, use the Union to create a union type that includes int and float: Starting from Python 3.10, you can use the X | Y syntax to create a union type, for example: Python allows you to assign an alias to a type and use the alias for type hintings. For example:
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. To determine how many items a tuple has, use the len () function: To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.
Tuple type; Tuple [X, Y] is the type of a tuple of two items with the first item of type X and the second of type Y. The type of the empty tuple can be written as Tuple [ ()]. Example: Tuple [T1, T2] is a tuple of two elements corresponding to type variables T1 and T2. Tuple [int, float, str] is a tuple of an int, a float and a string.
Yes, you can make the number of items in a tuple type hint flexible:
from typing import Tuple def func(var: Tuple[int, ...]): pass
From the docs: https://docs.python.org/3/library/typing.html#typing.Tuple
To specify a variable-length tuple of homogeneous type, use literal ellipsis, e.g.
Tuple[int, ...]
. A plainTuple
is equivalent toTuple[Any, ...]
, and in turn totuple
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With