I found many queries on python function arguments but still left confused. Suppose I want to pass few arguments in some function.
def anyFunc(name, age, sex = 'M', *cap_letters ):
print "name ", name
print "age ", age
print "sex ", sex
for s in cap_letters:
print "capital letter: ", s
name & age arguments are positional arguments. sex is default argument followed by variable length of non keyword arguments which are few random capital alphabets. So now if I run
anyFunc("Mona", 45, 'F', *('H', 'K', 'L'))
It gives me perfect output..
name Mona
age 45
sex F
capital letter: H
capital letter: K
capital letter: L
But if I pass below arguments where I want to use default sex value instead of passing it. Output is not as expeceted.
anyFunc("John", 45, *('H', 'K', 'L'))
name John
age 45
sex H
capital letter: K
capital letter: L
It should have taken the default value of sex i.e. 'M'. I also tried to pass sex argument at the very last but it gave me syntax error. Is there any way to achieve what I want?
sexa keyword-only argument. In Python 2.x you don't have that option, though.