Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the type hint for an array?

For most collections we can do:

from typing import List, Tuple, etc

Is there a hint for array? As in:

arr = array.array('i')
like image 558
Sergey Slepov Avatar asked Mar 14 '26 10:03

Sergey Slepov


1 Answers

As Carcigenicate pointed out, you can use array.array directly as a type annotation. However, you can't use array.array[int] or array.array[float] to specify the type of the elements.

If you need to do this, my suggestion is to use MutableSequence from collections.abc, since arrays implement all of the necessary operations: __len__, __getitem__, __setitem__, __contains__, __iter__, etc.

from collections.abc import MutableSequence

arr: MutableSequence[float] = array.array('f')
like image 146
Andrew Eckart Avatar answered Mar 15 '26 23:03

Andrew Eckart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!