Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass default & variable length arguments together in python?

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?

like image 486
Ankur Avatar asked Jan 20 '16 16:01

Ankur


People also ask

How do I set default value?

Set a default value Select the field that you want to change. On the General tab, type a value in the Default Value property box. The value you that you can enter depends on the data type that is set for the field. For example, you can type =Date() to insert the current date in a Date/Time field.

What is default arguments give example?

Key Points to Remember For Default Argument The overwriting of arguments take place when the calling function provides values for them. For example, calling of function sum(10, 15, 25, 30) facilitates overwriting of the value of z and w to 25 and 30 respectively.

Can we pass default arguments to overloaded functions?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

What do you mean by default argument?

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language).


1 Answers

Don't use * magic in a functions signature if you also use/need it on the calling side. Then simply drop it on both sides and don't make it more complicated as it has to be:

def any_func(name, age, sex='M', capital_letters=()):
    print 'name ', name
    print 'age ', age
    print 'sex ', sex
    for capital_letter in capital_letters:
        print 'capital letter: ', capital_letter

Called as:

any_func('Mona', 45, 'F', ('H', 'K', 'L'))

And with default sex:

any_func('John', 45, capital_letters=('H', 'K', 'L'))

If you don't like spelling out the capital_letters arguments name at many calls and it is acceptable to reorder the arguments, then swap the last two arguments:

def any_func(name, age, capital_letters=(), sex='M'):
    print 'name ', name
    print 'age ', age
    print 'sex ', sex
    for capital_letter in capital_letters:
        print 'capital letter: ', capital_letter

Calls:

any_func('Mona', 45, ('H', 'K', 'L'), 'F')
any_func('John', 45, ('H', 'K', 'L'))
like image 145
BlackJack Avatar answered Nov 09 '22 23:11

BlackJack