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

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -