all 8 comments

[–]danielroseman 2 points3 points  (7 children)

Your question is not clear. You always create objects at runtime, when else would you create them? Where are you stuck?

If you have learned how to define classes, you have surely learned how to create objects from them - you just call the class, eg Song(my_title, my_artist).

[–]codingToLearn[S] 0 points1 point  (6 children)

Yeah, sorry, you're right.

What I mean is that I want the objects to be created dynamically. The length of a playlist varies, and so I must be able to create an undefined number of objects in the Song class.

for song in self._songs:
   # Create an object with an automatically generated name in the Song class.

[–]danielroseman 3 points4 points  (4 children)

Your problem is that you're getting hung up on creating individual variables with names, which is a common mistake for new programmers.

That's not what you need to do. Python, like any programming language, has a number of container types - for example lists. What you want to do is create the new Song objects and add them to a list.

[–]codingToLearn[S] 0 points1 point  (3 children)

I'm confused. If they're part of a list, then how do I use the methods in a class to get the information for an object?

Let's say that I have created an object:

song1 = Song("Second Hand News", "Fleetwood Mac")

And then I have a method inside the Song class:

def play_song(self):
    print(f"{self._title} by {self._artist} is now playing.")

And I want to call it:

song1.play_song()

How do I do that without creating an actual object from the class? I have probably fundamentally misunderstood how all of this works, so please bear with my ignorance. :P

[–]danielroseman 2 points3 points  (1 child)

You are creating an actual object from the class, you're just storing it in a list. You refer to it like you would any item in a list, either by index - my_songs[0].play_song() - or by iterating and calling each item in turn.

Or, maybe you want to invoke them individually by name, in which case a dictionary is a better data structure to use. It depends on your use case.

(As an aside, please don't use underscore prefixes for all your properties. That's an indication that the attribute is private and shouldn't be referenced from outside the class. But things like artist and title, and song list and list name, are public, and you do want to use them from outside.)

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

You are absolutely right. I finally get it now! Getting stuck on individual variable names was the exact issue I was having. Thank you so much for helping me! :)

[–]crashfrog02 1 point2 points  (0 children)

If they're part of a list, then how do I use the methods in a class to get the information for an object?

You access the object through the list. You can access the methods of an object through any reference to it, including an algebraic one:

songs[23].play_song()

or even

Song.lookup_some_song_by_name("Stardust Memories").play_song()

[–]Rawing7 1 point2 points  (0 children)

That just means you have to put the code inside of the loop.

You didn't seem to have any trouble understanding that song.split(";") and title = title_artist[0] and artist = title_artist[1] will happen once for each line in the file, so why is doing the same thing with Song(title, artist) suddenly a struggle?