Hi I'm trying to understand how to implement optional arguments in a python function. For example, in the basic function below
def Ham(p,*q):
if q:
print p+q
else:
print p
Ham(2)
Ham(2,3)
I expect Ham(2) to return '2' which it does, however Ham(2,3) gives an error.
EDIT: Many thanks. Many of your answers were useful.
In your particular example, I think you mean to do:
That is, give
qa default value ofNoneand then only calculatep+qif aqis provided. Even simpler would be: