1

Is there any way to check the existence of argument of a function by assert statement?

def fractional(x) :
    assert x==None, "argument missing"    <---- is it possible here to check?
    assert type(x) == int, 'x must be integer'
    assert x > 0 , ' x must be positive '
    output = 1
    for i in range ( 1 , int(x)+1) :
        output = output*i
    assert output > 0 , 'output must be positive'
    return output 
y=3
fractional()   <----- argument missing

1 Answer 1

1

you shouldn't have to assert the existence of the argument explicitly. if the argument isn't given when you call the function, you'll get a TypeError like:

>>> def foo(x):
...     pass
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 1 argument (0 given)
>>>

if you wanted to ensure other properties of the argument (you mentioned only existence), you could test those properties and raise exceptions if they weren't met:

>>> def foo(x):
...    if not isinstance(x, str):
...        raise ValueError("argument must be a string!")
...
>>> foo(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in foo
ValueError: argument must be a string!
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.