exception - What is the best way to check that passed parameters are valid in Python? -


this question has answer here:

i've spent past year working in java, used assert ensure parameters passed method fulfilled preconditions. i'd same in python, read here exceptions better use assert.

this have currently:

if type(x) == list:     x = np.array(x) else:     err = ("object passed x not numpy.ndarray or list")     assert type(x) np.ndarray, err err = ("object passed n not integer") assert type(n) inttype, err err = ("the size of object passed x not equal "        "n squared") assert x.size n**2, err 

is there more elegant and/or pythonic way approach this? should writing own exception class raise when invalid parameters passed?

don't limit yourself

you astounded many functions, classes etc. produce useful output input more diverse in type original authors intended, long similiar enough orignal types. don't limit later unforeseen uses of code. known eafp-apporach.

so test know code produce senseless results.

assert doesn't work always

also assert won't when disable assertions (python -o).


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -