1 year ago

#380034

test-img

Chase

How do I define a python size generic?

I want to use a non-type parameter in Python, somewhat like this c++ snippet (this is only an example).

template<int SIZE>
class SizedData {
  void add(std::array<float,SIZE> point) { /* do stuff */ }
};

But in python, but as far as I can tell typing only supports typed generics parameters.

I want something like this, so that static analysis can find invalid arguments.

# Warning: This is not valid python

SIZE = SizeVar('SIZE', int) # allow any int as a valid value
class SizedData(Generic[SIZE]):
  def add(point: Annotated[Sequence[float], SIZE]) -> None:
    pass # do stuff

Thanks in advance for any help you can provide.

python

python-typing

0 Answers

Your Answer

Accepted video resources