Python - Create And Instantiate Class
I am building a class of playlists, which will hold many playlists of the same genre. class playlist(object): def __init__(self,name): self.name = name I would like
Solution 1:
If I understand correctly, you want playlists and users
class Playlist(object):
def __init__(self, name):
self.name = name
self.liked_by = list()
@classmethod
def get_genre(cls, genre):
# this relies on no instance of this class
pass
# return api data...
class User(object):
def __init__(self, name):
self.name = name
def likes_playlist(self, playlist):
playlist.liked_by.append(self.name)
And then, some examples
playlists = list()
hard_rock = Playlist('hard_rock')
joe = User('joe')
joe.likes_playlist(hard_rock)
playlists.append(hard_rock)
playlists.append(Playlist('pop_rock'))
country = Playlist.get_genre('country')
Solution 2:
Something like this would be possible if playlists are the core component of your application (very simple example using class inheritance).
>>> class playlist:
... def __init__(self,name, user):
... self.name = name
... self.user = user
...
>>> class hardrock(playlist):
... def __init__(self,name, user):
... playlist.__init__(self, name, user)
...
>>> test = hardrock('my_awesome_hardrock_list', 'my_awesome_username')
>>> print test.name
my_awesome_hardrock_list
>>> print test.user
my_awesome_username
You could start using just strings for users and later replace them with real objects and add some kind of relations between playlists and users. See cricket_007's suggestion for some more ideas.
Post a Comment for "Python - Create And Instantiate Class"