How is it possible that a list was given as a parameter to a function that expects a tuple?

Viewed 41

I was watching a tutorial on PyTorch and coding along and got stuck on function torch.randint According to the documentation:

torch.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

here, size is :

size (tuple) – a tuple defining the shape of the output tensor.

The YouTuber wrote

  random_idx = torch.randint(0, len(train_data), size=[1]).item()

But [1] is not a tuple, it is a list. How is this possible? I also tested it with a tuple and it worked just fine and every usage of randint() i found on the internet provides a tuple for size. E.g. size = (1,2) or size = (1,1).

I searched the source code for torch.randint but could not find it. I searched GitHub, PyTorch docs and even tried to find it in a local PyTorch library.

2 Answers

The documentation states that this should be a tuple however in practice, the definition of randint() is:

def randint(low: _int, high: _int, size: _size, ... )

Where _size is defined as:

(type alias) _size: Type[Size] | Type[List[int]] | Type[Tuple[int, ...]]

So in practice, the requirement is for the size parameter to be of type Size, List of int or Tuple of int, which will pretty much behave the same in this case

EDIT: As stated above, indeed, typing is only an indicative in Python, so if you use any type of variable, there won't be any issue if the function itself doesn't raise an error. For the question of why the function acts accordingly and returns what is expected, this is because of the first part of the answer :)

The only difference between a list and a tuple is that you cannot modify a tuple once it has been created. If you want the array to be immutable, you'd want to change it to a tuple yes, but otherwise, all read operations between lists and tuples are identical, so your code will work just fine :).

Related