Positional Argument V.S. Keyword Argument

Positional argument vs keyword argument

That text you quote seems to be confused about two totally different things:

  • Positional and keyword arguments are a feature of calls to a function (see Python reference section 5.3.4 Calls).
  • Default values are a feature of function definitions, as per section 7.6 Function definitions

I suspect the people who put together that course-ware weren't totally familiar with Python :-) Hence that link you provide is not a very good quality one.


In your call to your function, you're using the "keyword argument" feature (where the argument is named rather than relying on its position). Without that, values are bound to names based on order alone. So, in this example, the two calls below are equivalent:

def process_a_and_b(a, b):
blah_blah_blah()

process_a_and_b(1, 2)
process_a_and_b(b=2, a=1)

By further way of example, refer to the following definition and calls:

def fn(a, b, c=1):        # a/b required, c optional.
return a * b + c

print(fn(1, 2)) # returns 3, positional and default.
print(fn(1, 2, 3)) # returns 5, positional.
print(fn(c=5, b=2, a=2)) # returns 9, named.
print(fn(b=2, a=2)) # returns 5, named and default.
print(fn(5, c=2, b=1)) # returns 7, positional and named.
print(fn(8, b=0)) # returns 1, positional, named and default.

Order of using positional and keyword arguments

Positional arguments must be passed in order as declared in the function. So if you pass three positional arguments, they must go to the first three arguments of the function, and those three arguments can't be passed by keyword. If you want to be able to pass the first argument out of order by keyword, all your arguments must be passed by keyword (or not at all, if they have defaults).

If it helps, Python's binding mechanism is roughly:

  1. Assign positional arguments one by one to sequential parameters of the function. These parameters are now set.
  2. Assign keyword arguments to remaining parameters in any order. If one of the keyword arguments matches an argument already assigned positionally (or the same keyword argument is passed twice), it's an error.

In your case, what this means is that:

greet('Holmes', first_name='Harry')

first binds 'Holmes' to first_name. Then it saw you tried to pass first_name again as a keyword argument and objected.

Normal arguments vs. keyword arguments

There are two related concepts, both called "keyword arguments".

On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which were not mentioned at all.

The other concept is on the function definition side: you can define a function that takes parameters by name -- and you don't even have to specify what those names are. These are pure keyword arguments, and can't be passed positionally. The syntax is

def my_function(arg1, arg2, **kwargs)

Any keyword arguments you pass into this function will be placed into a dictionary named kwargs. You can examine the keys of this dictionary at run-time, like this:

def my_function(**kwargs):
print str(kwargs)

my_function(a=12, b="abc")

{'a': 12, 'b': 'abc'}

Positional argument follows keyword argument, not sure how to resolve this

your code look ok you just need to change how you send the part param.

you need a commaseparated string not sevral strings seperated by a comma.

import pandas as pd 
import seaborn as sns

api_key = 'API_KEY'
channel_id = 'CHANNEL_ID'

youtube = build('youtube','v3', developerKey=api_key)

def get_channel_stats (youtube, channel_id):
request = youtube.channels().list(
part='snippet,contentDetails,statistics', id=channel_id)
response = request.execute()
return response

Why positional arguments need to be specified before keyword arguments in Python?

See it seems your argument is valid.

But I like to point out when you said, "Isn't this intuitive:"

Tell me any other object oriented language where you have seen this, and is quite popular.

So the thing is python is popular because it is very easy to adapt to,

I don't know about you but I switched to python recently from C, and there I use to

create functions like length, reverse, sort etc. in python the exact most common names are present of the similar function. It is so easy to adapt, unlike other language where they change name or insert these functions hidden in some utils class like java.

To answering your question.
Like other say it will increase complexity.

And this is why other popular object oriented language didn't adopt it.

And so since its not available in other popular language.

Python didn't adopt it, cause it would make adapting to python difficult. And also due to underlying reason that it would make things complex.

"Isn't this intuitive:" for me, politely, NO, it is not intuitive.

If I pass argument at position 0 in function call, it would expect it to go to position 0 argument in function defination. Cause That's how I did in C. And I believe cause that's how its done in most object oriented language.



Related Topics



Leave a reply



Submit