Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit function parameter as array of fixed-size?

Tags:

python

arrays

How can I limit python function parameter to accept only arrays of some fixed-size?

I tried this but it doesn't compile:

def func(a : array[2]):

with

TypeError: 'module' object is not subscriptable

I'm new to this language.

like image 268
AnArrayOfFunctions Avatar asked Apr 19 '15 17:04

AnArrayOfFunctions


1 Answers

What about checking the length inside of the function? Here I just raised an error, but you could do anything.

def func(array):
    if len(array) != 2:
        raise ValueError("array with length 2 was expected")
    # code here runs if len(array) == 2
like image 71
Totem Avatar answered Oct 24 '22 22:10

Totem