Veneer Buy_Artwork Method Help

When I try using the buy_artwork() method, it does nothing. Can someone give me some pointers? Here is a sample of my code:

  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)

…print?

After using the buy_artwork function I had tried printing out girl_with_mandolin and it had the same owner, when the project said it should have updated to the MOMA. What have I done wrong?

but it’s not just after calling the function that you can print out information, you can print out before to see if the input is as you expect and you can print out while the function is executing to observe the individual steps carried out

I tried printing girl_with_mandolin out immediately before calling the function. It looks exactly the same as it does after calling the function. What do you mean by

? Do you mean print(moma.buy_artwork(girl_with_mandolin))? When I tried that, it just output None. Would it help if I showed you my entire code?

Your code is supposed to do something. There are conditions to consider and so on.

All of that is observable, so if you print out the things carried out, you can identify which particular operation wasn’t carried out right.

You say that girl_with_mandolin looks exactly the same before and after. Should it look the same? What should have changed? If so, why should it have changed, what decisions should have been involved?

what did you say? what did you decide should happen?

you’re looking at the result after having called the function, but you carried out lots of steps, you can look at things during more points in time

Everything seemed to be working right up to this point in the code. Here is my entire code in case it helps:

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 "{}. {}. {}, {}. {}, {}.".format(self.artist, self.title, self.year, self.medium, self.owner.name, self.owner.location)
class Marketplace:
  def __init__(self, listings):
    self.listings = []
  def add_listing(self, new_listing):
    self.listings.append(new_listing)
  def remove_listing(self, listing):
    self.listings.remove(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(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 "{}. {}.".format(self.art, self.price)

veneer = Marketplace([])
edytta = Client("Edytta Halpirt", "Private Collection", False)
girl_with_mandolin = Art("Picasso, Pablo", "'Girl with a Mandolin (Fanny Tellier)'", 1910, "oil on canvas", edytta)
moma = Client("The MOMA", "New York", True)
edytta.sell_artwork(girl_with_mandolin, "$6M (USD)")
print(girl_with_mandolin)
moma.buy_artwork(girl_with_mandolin)
print(girl_with_mandolin)

Is this the line that would have the effect of making that change?

          art_listing.art.owner = self

then, you would have to run it, or it wouldn’t happen, right? so you’d start by checking whether it gets executed.

          print('changing owner')
          art_listing.art.owner = self

that would be making an observation about the steps you are carrying out. what other steps do you carry out? those can be observed too.

You might also want to show the effect, not just that it happens. you might for example expect that the string description changes, so you might print out the string description before and after making that change

          print('changing owner')
          print(art_listing)
          art_listing.art.owner = self
          print(art_listing)
2 Likes

By Jove, the line never ran at all! Why is that?

AH! I finally caught my mistake! I indented it wrong! Thank you so much!

this should remind you of namedtuple, automatically creating a class with fields and string representations
a small caveat though, namedtuple is immutable (because it’s a tuple)
but there’s also dataclass which does something similar

the client doesn’t really have methods either, buy and sell arguably belong to Marketplace

from dataclasses import dataclass


@dataclass(frozen=True)
class Client:
    name: str
    location: str
    is_museum: bool


@dataclass
class Art:
    artist: str
    title: str
    year: int
    medium: str
    owner: Client


@dataclass(frozen=True)
class Listing:
    art: Art
    price: str


class Marketplace:
    def __init__(self):
        self.listings = {}

    def show_listings(self):
        return repr(list(self.listings.values()))

    def sell_artwork(self, artwork: Art, price: str):
        self.listings[id(artwork)] = Listing(artwork, price)

    def buy_artwork(self, artwork: Art, buyer: Client):
        self.listings.pop(id(artwork)).art.owner = buyer


veneer = Marketplace()
edytta = Client("Edytta Halpirt", "Private Collection", False)
girl_with_mandolin = Art(
    "Picasso, Pablo",
    "'Girl with a Mandolin (Fanny Tellier)'",
    1910,
    "oil on canvas",
    edytta,
)
moma = Client("The MOMA", "New York", True)
veneer.sell_artwork(girl_with_mandolin, "$6M (USD)")

print(girl_with_mandolin)
veneer.buy_artwork(girl_with_mandolin, moma)
print(girl_with_mandolin)
1 Like