python - defining functions with optional arguments -


hi i'm trying understand how implement optional arguments in python function. example, in basic function below

def ham(p,*q):     if q:         print p+q     else:         print p  ham(2)   ham(2,3) 

i expect ham(2) return '2' does, ham(2,3) gives error.

edit: many thanks. many of answers useful.

in particular example, think mean do:

def ham(p,q=none):     if q:         print p+q     else:         print p 

that is, give q default value of none , calculate p+q if q provided. simpler be:

def ham(p,q=0):     print p+q 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -