Home > Blockchain >  Positional argument follows keyword argument, not sure how to resolve this
Positional argument follows keyword argument, not sure how to resolve this

Time:04-06

How do I fix this error ?

Code:

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

Error message:

SyntaxError: positional argument follows keyword argument

How do I avoid this error? I'm making a silly mistake somewhere but not sure how to fix it.

CodePudding user response:

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

CodePudding user response:

Assuming the rest of the arguments to youtube.channels().list() are in the correct order, you just need to move part = 'snippet' over. The parser expects to find all positional arguments first (ones where the parameter name is not specified), so any argument with the <name>= syntax must come at the end.

The reason for this is that many functions accept *args and **kwargs, the point of these being to allow for arbitrary numbers of arguments. The only way to ensure unnamed arguments get assigned to the right place is to be strict about their order and placement in the function call.

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(
            'contentDetails','statistics', part= 'snippet', id=channel_id)
    response = request.execute()
    return response

CodePudding user response:

So actually this error is raised in python when you have used mix of keyword and positional arguments while function calling .strong text

You have to call function in such a way that all positional arguments comes first before any keyword arguments in sequence.

you can solve it by updating your function get_channel_stats below:

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

Hope it will solve the issue .

  • Related