Advertisement
karlakmkj

Instance reference - Exercise 1

Jan 5th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. '''
  2. Problem: Notice that song_1 and song_2 have the same artist and label. That means they should each have the SAME instance of artist: do not create separate instances of artist for each song.
  3. '''
  4.  
  5. class Artist:
  6.     def __init__(self, name, label):
  7.         self.name = name
  8.         self.label = label
  9.  
  10. class Song:
  11.     def __init__(self, name, album, year, artist):
  12.         self.name = name
  13.         self.album = album
  14.         self.year = year
  15.         self.artist = artist
  16.        
  17. #Write your code here!
  18. artist1 = Artist("Taylor Swift", "Big Machine Records, LLC")
  19. song_1 = Song("You Belong With Me", "Fearless", 2008, artist1)
  20. song_2 = Song("All Too Well", "Red", 2012, artist1)
  21. #Alternative way to reference song_1 artist since they belong to the same artist
  22. #song_2 = Song("All Too Well", "Red", 2012, song_1.artist)
  23.  
  24. #We can also create a variable for artist2 and do it the same way as I did for song_1
  25. song_3 = Song("Up We Go", "Midnight Machines", 2016, Artist("LiGHTS", "Warner Bros. Records Inc."))
  26.  
  27. #Feel free to add code to test your code below.
  28. print(song_1.artist.name)
  29. print(song_2.artist.label)
  30. print(song_3.artist.name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement