I don’t understand two things:
def sell_artwork(self, artwork, price):
if artwork.owner == self: **<----------------1) owner is a part of the Art class not Client class so how can we use artwork.owner here? Without Inheritance etc?**
new_listing = Listing(artwork, price, self)
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: **<----------------- 2) Same thing here veneer.listing is a part of the Marketplace class which doesn't have an art parameter, so how is listing.art there?**
art_listing = listing
break
if art_listing != None:
art_listing.art.owner = self
veneer.remove_listing(art_listing)
Full code
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"{self.artist}. {self.title}. {self.year}, {self.medium}. {self.owner.name}, {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, ex_listing):
self.listings.remove(ex_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.is_museum = is_museum
if is_museum:
self.location = location
else:
self.location = "Private Collection"
def sell_artwork(self, artwork, price):
if artwork.owner == self:
new_listing = Listing(artwork, price, self)
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 f"{self.art.title}, {self.price}"
veneer = Marketplace()
veneer.show_listings()
edytta = Client("Edytta Halpirt", None, False)
moma = Client("The MOMA", "New York", True)
girl_with_mandolin = Art("Picasso, Pablo", "Girl with a Mandolin (Fanny Tellier)", "oil on canvas", 1910, moma)
mc = Art("M.C.Escher", "Infinite Stairwell", "oil on canvas", 1895, edytta)
moma.sell_artwork(girl_with_mandolin, "$6 million (USD)")
veneer.show_listings()
edytta.buy_artwork(girl_with_mandolin)
print(girl_with_mandolin)
print(mc)
print(veneer.show_listings())