function - Argument existence by assert statement in python -
is there way check existence of argument of function assert statement?
def fractional(x) : assert x==none, "argument missing" <---- possible here check? assert type(x) == int, 'x must integer' assert x > 0 , ' x must positive ' output = 1 in range ( 1 , int(x)+1) : output = output*i assert output > 0 , 'output must positive' return output y=3 fractional() <----- argument missing
you shouldn't have assert existence of argument explicitly. if argument isn't given when call function, you'll typeerror like:
>>> def foo(x): ... pass ... >>> foo() traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: foo() takes 1 argument (0 given) >>>
if wanted ensure other properties of argument (you mentioned existence), test properties , raise exceptions if weren't met:
>>> def foo(x): ... if not isinstance(x, str): ... raise valueerror("argument must string!") ... >>> foo(42) traceback (most recent call last): file "<stdin>", line 1, in <module> file "<stdin>", line 3, in foo valueerror: argument must string! >>>
Comments
Post a Comment