Using default arguments before positional arguments

I am learning to use positional arguments in python and also trying to see how they work when mixed up with default arguments:-

def withPositionalArgs(ae=9,*args):
    print 'ae= ', ae
    print 'args = ', args


a=1
b=2
c=[10,20]

withPositionalArgs(a,b,c)

This gives me the output:

ae=  1
args =  (2, [10, 20])

As you can see, a is considered to be a value passed for ae, and b as well as c are considered to be the positional arguments.

So, I am now trying to assign 10 for ae while calling withPositionalArgs:

withPositionalArgs(ae=10,b,c)

But, I can not do it. I get the error:

SyntaxError: non-keyword arg after keyword arg

My question is:

Am I doing correctly? Is having default argument allowed or a good practice to use before positional arguments in python functions?

Answers


In Python2, you are not allowed to put arguments which have a default value before positional arguments.

The positional arguments must come first, then the arguments with default values (or, when calling the function, the keyword arguments), then *args, and then **kwargs.

This order is required for both the function definition and for function calls.

In Python3, the order has been relaxed. (For example, *args can come before a keyword argument in the function definition.) See PEP3102.


Need Your Help

Differences between javascript function

javascript html html5

I know this question might be silly just would like to know, and I am not sure whether the topic matches to my current asking. But still thought to ask it.

Dynamically create array - could not deduce template argument for 'T' - C++

c++ templates

I am trying to create an array of ints, doubles, or strings depending on what the user inputs. The program asks the user how many objects are in the array and then asks for the objects. After th...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.