CREATE AN ART MARKETPLACE Veneer

Hi everyone, in my code below, can anyone please quickly explain how ‘artwork.owner’ in the method .sell_artwork of my Client class is able to access the owner parameter of my Art class?

same with the {self.art.title} in the ‘repr’ of my Listing class. how is title retrieved from the Art class?

i cant make the connection. Please help me understand

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 f'''
artist's name: {self.artist}
network name : "{self.title}"
medium : {self.medium}
year created : {self.year}
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, expired_listing):
    self.listings.remove(expired_listing)

  def show_listings(self):
    for listing in self.listings:
      print(self.listings)  

class Client:
  def __init__(self, name, location, is_museum):
    self.name = name
    self.is_museum = is_museum
    if is_museum:
      self.location = location
    else: 
      self.location = "Private Collection"      

  def __repr__(self):
    return f'''
{self.name}      
  It is a museum: {self.is_museum} 
  {self.location}
  '''
  def sell_artwork(self, artwork, price):
    if artwork.owner == self:
      new_listing = Listing(artwork, price, self)
      veneer.add_listing(new_listing)
      



class Listing:
  def __init__(self, art, price, seller):
    self.art = art
    self.price = price
    self.seller = seller

  def __repr__(self):
    return f'''
    {self.art.title}
    {self.price}
      
     '''

   
veneer = Marketplace()

#print(veneer.show_listings())





edytta = Client("Edytta Halprint", "collector", 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)

#print(moma, edytta)
#print(girl_with_mandolin)


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

veneer.show_listings()

result prints:

[
    Girl with a Mandolin (Fanny Tellier)
    $6M (USD)
      
     ]

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

1 Like

Hi @seppp,

Whenever you create an instance of Art, its __init__ method creates an owner variable that belongs to that instance, as follows:

    self.owner = owner

Thereafter, you can use dot notation to access the owner instance variable wherever that instance of Art is accessible. Within the sell_artwork method of Client, if an instance of Art is represented by the variable artwork, that instance is guaranteed to have an owner instance variable, and that variable is accessible as artwork.owner.

Similarly, in the Listing class or anywhere else, every instance of Art has all the instance variables that were created by the __init__ method of Art, and those variables are accessible via dot notation.

2 Likes

oh ok i think i got it, and for the string:

def sell_artwork(self, artwork, price):
    if artwork.owner == self:
      new_listing = Listing(artwork, price, self)
      veneer.add_listing(new_listing)

we created an instance of Listing inside the Client class only accessible to client, cuz there is no other instance for Listing.

thank you very much

Yes, the variable, new_listing, that is created here is local to the method, and is only recognized in the sell_artwork method:

      new_listing = Listing(artwork, price, self)

However, here we pass that instance to a method called from veneer, making that instance of Listing available to the veneer object within its listings instance variable:

      veneer.add_listing(new_listing)
2 Likes

yes thats right got it finally! thanks for the explanation and help

1 Like