2016-11-13 4 views
0

assert 문을 사용하여 함수의 인수를 확인할 수있는 방법이 있습니까?파이썬에서 assert 문에 의한 인수 존재

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

명시 적으로 인수의 존재를 주장 할 필요가 없습니다. , 당신이 그 속성을 테스트 할 수는 인수의 기타 속성을 확인하고 싶다면

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

가 (당신은 단지 존재를 언급) : 인수가 함수를 호출 할 때 지정되지 않는 경우는, 당신은 같은 형식 오류를 얻을 수 있습니다 충족되지 않으면 예외를 발생시킵니다.