Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

python - pyhton __init__() takes 1 positional argument but 2 were given newbie

I have an attribute error in a little spotify-api program i am trying to run

my run file contains the following

import os

from spotify_client import AddSongs


def run():
    spotify_client = AddSongs('spotify_token')
    random_tracks = spotify_client.get_random_tracks()
    track_ids = [track['id'] for track in random_tracks]

    was_added_to_queue = spotify_client.add_tracks_to_queue()
    if was_added_to_queue:
        for track in random_tracks:
            print(f"Added {track['name']} to your library")


if __name__ == '__main__':
    run()

then in spotify_client is the following class

class AddSongs(object):
    def __init__(self,):
        self.spotify_token = ""
        self.uri = ""



    def get_random_tracks(self):
        wildcard = f'%{random.choice(string.ascii_lowercase)}%'
        query = urllib.parse.quote(wildcard)
        offset = random.randint(0, 2000)
        url = f"https://api.spotify.com/v1/search?q={query}&offset={offset}&type=track&limit=1"
        response = requests.get(
            url,
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {self.spotify_token}"
            }
        )
        response_json = response.json()
        print(response)

        tracks = [
            track for track in response_json['tracks']['items']
        ]
        self.uri = response_json["tracks"]["items"][0]["uri"]
        


        
        print(f'Found {len(tracks)} tracks to add to your queue')

        
        
        return tracks
        return self.uri

    def add_tracks_to_queue(self,):

            print('adding to queue...')
            url =f"https://api.spotify.com/v1/me/player/queue?uri={self.uri}"
            response = requests.post(
            url,
            
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {self.spotify_token}"
            }
        )

            print(f"Added {track['name']} to your queue")

            return response.ok 

    def callrefresh(self):

        print("Refreshing token")

        refreshCaller = Refresh()

        self.spotify_token = refreshCaller.refresh()

        self.get_random_tracks()

        
a = AddSongs()
a. callrefresh()

As you can see it runs the code fine up untill add_tracks_to_queue this is giving me the following traceback

Refreshing token
    <Response [200]>
    Found 1 tracks to add to your queue
    Traceback (most recent call last):
      File "/Users/shakabediako/Documents/free_streams/run.py", line 18, in <module>
        run()
      File "/Users/shakabediako/Documents/free_streams/run.py", line 7, in run
        spotify_client = AddSongs('spotify_token')
    TypeError: __init__() takes 1 positional argument but 2 were given
    >>>

I know there are multiple threads about this error but after reading most of them i haven't been able to understand the concept or find the answer. I think it has something to do with me calling the function from another file but i do not understand why this creates another "positional argument" I know this because if i just run the spotify_client file i get the following response

Refreshing token
<Response [200]>
Found 1 tracks to add to your queue
>>> 

which are just my print values up until def add_tracks_to_queue (which i also don't understand why it is not running)

I really hope someone can help me with this Thanks in advance

question from:https://stackoverflow.com/questions/65831337/pyhton-init-takes-1-positional-argument-but-2-were-given-newbie

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your AddSongs class doesn't accept any variables as input (also there's a stray comma in the __init__ which doesn't seem to serve any purpose):

class AddSongs(object):
    def __init__(self,):

Yet you're calling it as if it does accept a variable:

spotify_client = AddSongs('spotify_token')

The self parameter has to be in there, but if you want to accept any other variables, you need to actually define them. The solution could therefore be to add a token parameter:

class AddSongs(object):
    def __init__(self, token=""): # defaults to an empty string if token is not passed in
        self.spotify_token = token

That way, if you call AddSongs('spotify_token'), the class's self.spotify_token will get set to 'spotify_token'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...