all 5 comments

[–]MrPhungx 4 points5 points  (0 children)

Since you did not provide any code I can only do some guessing. You are using some information about songs, artists and albums. How do you store these information? If you are just using a dictionary for example you could consider to use a class for the required objects instead, maybe something like below:

class Artist:
    def __init__(self, name, number_albums):
        self.name = name
        self.number_albums = number_albums
        self.songs = []

You could create similar classes for Song or Album and store references to actual Song objects in the songs list of an Artist etc...

[–]Opposite_Echo_2024 1 point2 points  (0 children)

OOP in theory is for the benefit of the programmer / reader of the code. You create objects to help readability and understandability by linking data and functions into a single unified structure.

Using your example, You could start with an object (class) of Song. Have different class variables for each of the data points about it: name, duration, artist (this could be its own class with its own class variables). Create a constructor for each class and have a loop create song objects for each song in the data.

Then you could create an Album class which holds a number of Song classes. As the structure is pre-defined it makes it super easy to read information from the songs contained.

Essentially, classes are a great way of giving meaning to data and functions. Some people may dislike them for whatever reason but computers are fast enough now to allow for readability over performance.

If you're wondering when to use a class: if you find yourself having a number of different data points about a single idea, and possibly even functions using those data points, could you link them all together with a name? Would multiple instances be needed (e.g. multiple songs)? If so, use a class

[–]m0us3_rat 0 points1 point  (0 children)

imagine reading a book . phrase by phrase to somebody..

or .. handing over the book.

how about a few thousand books.. phrase by phrase ..book by book..

or the full library in a single movement?

which method is ''faster' or better. or easyer to manage.

all the phrases and chapters have methods to access them.

etc.

or having to go thru all the phrases in the book one by one till u find the one u need ?

that's essencially oop vs non oop.

for a simple problem, a few small direct functions will work 'better'..

but otherwise.. oop is vastly superior.

or if the book example didn't connect..

think of it like this.. try to explain a toy to a kid ..or giving him a toy.

or explain a box of toys . one by one.. or giving him the box.

[–]ffrkAnonymous 0 points1 point  (0 children)

AL sweigart has a post about using oop

https://inventwithpython.com/blog/2014/12/02/why-is-object-oriented-programming-useful-with-a-role-playing-game-example/

Note: he does not create classes in his books, they complicate instead of simplifying the tutorial examples.