Veneer project - buy_artwork function won't work


#1

https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-art-marketplace/modules/cspath-veneer/projects/veneer

Hello,
I’m working on the Veneer project and am having trouble getting my buy_artwork function to work. It seems to have no effect whatsoever. Everything up to this point was working so if anyone knows what could be causing this, let me know. Thanks!

class Art:
  def __init__(self, artist, title, medium, year, owner):
    self.artist = artist
    self.title = title
    self.medium = medium
    self.year = year
    self.owner = owner
    
  def __repr__(self):
    return '{artist}. "{title}". {year}, {medium}. {owner}, {location}.'.format(artist=self.artist, title=self.title, year=self.year, medium=self.medium, owner=self.owner.name, location=self.owner.location)

class Marketplace:
  def __init__(self):
    self.listings = []
  
  def add_listing(self, new_listing):
    self.listings.append(new_listing)
  
  def remove_listing(self, old_listing):
    self.listings.remove(old_listing)
    
  def show_listings(self):
    for listing in self.listings:
      print(listing)
      
class Client:
  def __init__(self, name, location, is_museum):
    self.name = name
    self.location = location
    self.is_museum = is_museum
    
  def sell_artwork(self, artwork, price):
    if artwork.owner == self:
      new_listing = Listing(self, artwork, price)
      veneer.add_listing(new_listing)

  def buy_artwork(self, artwork):
    if artwork.owner != self:
      art_listing = None
      for listing in veneer.listings:
        if listing.art == artwork:
          art_listing = listing
          break
    if art_listing != None:
      art_listing.art.owner = self
      veneer.remove_listing(art_listing)

class Listing:
  def __init__(self, art, price, seller):
    self.art = art
    self.price = price
    self.seller = seller
   
  def __repr__(self):
    return "%s, %s" %(self.art.name, self.price)
    
edytta = Client("Edytta Halpirt", "Private Collection", False)
moma = Client("The MOMA", "New York", True)   
    
girl_with_mandolin = Art("Picasso, Pablo", "Girl with a Mandolin (Fanny Tellier)", "oil on canvas", "1910", (edytta))

veneer = Marketplace()

edytta.sell_artwork(girl_with_mandolin, "$6M (USD)")

moma.buy_artwork(girl_with_mandolin)

print(girl_with_mandolin)

#2

Hi @bbalagia

Consider this statement within the sell_artwork method of Client:

      new_listing = Listing(self, artwork, price)

What objects are being passed, via the arguments, to the __init__ method of Listing? For example, what does self represent in that statement?

Edited on February 12, 2019 to revise the questions asked above


#3

self represents the Listing class, right?


#4

The statement that we are discussing is part of the sell_artwork method of the Client class. That method has this header:

  def sell_artwork(self, artwork, price):

Therefore, in the statement under consideration, self represents the instance of Client from which the sell_artwork method is being called, rather than the instance of Listing that is involved in the sale. The instance of Client represented by self is the seller of the work of art.

Since we are creating an instance of Listing, we need to consider what arguments to pass to the __init__ method of Listing. This is the header of that method:

  def __init__(self, art, price, seller):

Note that the final parameter is seller. That parameter needs to receive the reference to the Client who is selling the art, namely self. So, here is how we make the call, matching the arguments to the parameters of the appropriate __init__ method:

      veneer.add_listing(Listing(artwork, price, self))