data_garden data_garden - 1 year ago 91
Python Question

Python - Create and instantiate class

I am building a

of playlists, which will hold many playlists of the same genre.

class playlist(object):
def __init__(self,name): = name

I would like to instantiate them passing the user:

def hard_rock(self,user):
self.user = user
#query and retrieve data from music API
#return playlist

def pop_rock(self,user):
self.user = user
#query and retrieve data from music API
#return playlist

#and so on

create instance:

r = playlist('rock')

is this a logical way of building and instantiating classes?

Answer Source

If I understand correctly, you want playlists and users

class Playlist(object):
    def __init__(self, name): = name
        self.liked_by = list()

    def get_genre(cls, genre):
        # this relies on no instance of this class
        # return api data...

class User(object):
     def __init__(self, name): = name

     def likes_playlist(self, playlist):

And then, some examples

playlists = list()
hard_rock = Playlist('hard_rock')

joe = User('joe')


country = Playlist.get_genre('country')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download