Python - Create and instantiate class

2024/7/8 8:16:27

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 to instantiate them passing the user:

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

create instance:

r = playlist('rock')
r.hard_rock('user1')

is this a logical way of building and instantiating classes?

Answer

If I understand correctly, you want playlists and users

class Playlist(object):def __init__(self, name):self.name = nameself.liked_by = list()@classmethoddef get_genre(cls, genre):# this relies on no instance of this classpass# return api data...class User(object):def __init__(self, name):self.name = namedef 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')
https://en.xdnf.cn/q/119450.html

Related Q&A

Missing samples of a dataframe in pandas

My df:In [163]: df.head() Out[163]: x-axis y-axis z-axis time 2017-07-27 06:23:08 -0.107666 -0.068848 0.963623 2017-07-27 06:23:08 -0.105225 -0.070068 0.963867 .....I set the index as dateti…

How to hide a button after clicked in Python

I was wondering how to hide my start button after being clicked so that If the user accidentally was clicker happy they wouldnt hit the button causing more bubbles to appear on screen. Below is a snipp…

Unable to click on QRadioButton after linking it with QtCore.QEventLoop()

Few days back i had situation where i had to check/uncheck QRadioButton in for loop. Here is the link Waiting in for loop until QRadioButton get checked everytime? After implementing QEventLoop on thi…

Distance Matrix Haversine

I am working on a data frame that looks like this :lat lon id_zone 0 40.0795 4.338600 1 45.9990 4.829600 2 45.2729 2.882000 3 45.7336 4.850478 4 45.6981 5.…

python google geolocation api using wifi mac

Im trying to use Googles API for geolocation giving wifi data to determine location. This is their intro. And this is my code@author: Keith """import requestspayload = {"c…

Python requests and variable payload

Reticulated members,I am attempting to use a GET method that is supported against the endpoint. However, I am using python and wanting to pass the user raw_input that is assigned to a variable:uid = ra…

How should I read and write a configuration file for TkInter?

Ive gathered numbers in a configuration file, and I would like to apply them to buttons. Clicking the button should allow the number to be changed and then re-written to the config file. My current cod…

How to convert this nested dictionary into one single dictionary in Python 3? [duplicate]

This question already has answers here:Convert nested dictionary into a dictionary(2 answers)Flatten nested dictionaries, compressing keys(32 answers)Closed 4 years ago.I have a dictionary like this:a …

How to click unopened tabs where the numbers change

How do I click all the unopened tabs pages where the value changes when you click tabs? (see image below)Take the following script, based off of this question with the following approach:clickMe = wai…

Xlwings / open password protected worksheet in xlsx?

I get an answer how to open a password protected xlsx with xlwings: Xlwings / open password protected xlsx? wb = xlwings.Book("file.xlsx", password="Passw0rd!")But can i also open …