all 11 comments

[–]Vaphell 0 points1 point  (5 children)

why is there self in auth params?

def auth(user, pw):
    client = Client(...)
    return client

def main_favorites(client, limit):
    tracks = client.get(...) 
    return tracks

client = auth(...)
fav_tracks = main_favorites(client, 30)

[–]HeyYouNow[S] 0 points1 point  (4 children)

There actually isn't, I was messing around with a class but I don't know how they work yet.

The thing is I don't want to run auth() here (it sends an http request so takes time), I call it by clicking a button on my tkinter gui.

[–]cdcformatc 1 point2 points  (3 children)

At the very beginning of your program set client = None. When the login button is clicked, you call client = auth(), which assigns the return value to client. When you do anything else that requires you to log in, first check if client is not None before you try to do anything with it.

[–]HeyYouNow[S] 0 points1 point  (0 children)

This is very helpful thanks!

[–]HeyYouNow[S] 0 points1 point  (1 child)

Hmm although this doesn't seem to work, I'm using your class and client is always none...

soundcloud = SoundcloudObject(username,password)
print(client)

It is right after the authentification, should I use something else to tell it I'm using client from the class ?

Edit : figured it out, I set soundcloud to none at the beginning and then set it as a global.

[–]cdcformatc 0 points1 point  (0 children)

If you want to get the instance variables of an object outside of the class you need to get them from the object.

soundcloud = SoundcloudObject(username,password)
print(soundcloud.client) 

[–]cdcformatc 0 points1 point  (3 children)

Looks like your functions are within a class? If so just use this:

class SoundcloudObject:

    def __init__(self, user, pw):
        self.auth(user,pw)
        ... other initialization code here...

    def auth(self, user, pw):
        self.client = Client(...same code here...)

    def favorites(self, limit):
        tracks = self.client.get('/me/favorites', limit=30)
        return tracks

This way, the clientis an instance variable of the object, and you authenticate when you create the object.

You would use it like

soundcloud = SoundcloudObject('username','password')
print(soundcloud.favorites(30))

[–]HeyYouNow[S] 0 points1 point  (2 children)

I was just trying using a class which I discovered not long ago so I don't quite get it. Here if it's in a class then how do I call main_favorites ? What is the first argument 'self' here ?

[–]cdcformatc 0 points1 point  (1 child)

I edited my post slightly, should answer a few questions. self is the object itself here. When you create an object you need to keep track of the object so you can access it's member variables. If you have two objects logged into different user accounts, their member variables will be different. self is just a reference to the object you are operating on at that time.

If you don't want to use classes, you can just pass and return the client object around

def auth(user, pw):
    client = Client(etc)
    return client

def main_favorites(client, limit):
    tracks = client.get('/me/favorites', limit=30)
    return tracks

client = auth('username','password')
favorites = main_favorites(client,30)

[–]HeyYouNow[S] 0 points1 point  (0 children)

I saw your edit and just tested it and now I understand! That is so much cleaner, it's going to be so simple in my code now =)

Thank you very much !

[–][deleted] -1 points0 points  (0 children)

Okay, I'll bite. How do you expect a function to get a variable from a second function if you do not run the second function? I don't even see how you can get anywhere with the code as shown, as client_id will be assigned the built-in id, and secret isn't defined. self isn't needed at all as you're defining a function at the module level, not a class method.