Veneer Error: __str__ returned non-string (type NoneType)

Hello,

I am trying to understand by my code is returning:
Traceback (most recent call last):
File “script.py”, line 57, in
veneer.show_listings()
File “script.py”, line 20, in show_listings
print(listing)
TypeError: str returned non-string (type NoneType)

when i call:
edytta.sell_artwork(girl_with_mandolin, “6M (USD)”)
and
veneer.show_listings()

I have spent a good day trying to figure out the error myself but i have no idea. Please let me know what i might of done wrong or if i can put this a better way. Thank you!

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 'The item is by {}, called "{}" from {}, {} and is owned by {}'. format(self.artist, self.title, self.year, self.medium, self.owner)
class Marketplace:
  def __init__(self):
    self.listings = []    
  def add_listing(self, new_listing):
    self.listings.append(new_listing)
  def remove_listing(self, remove_listing):
    self.listings.remove(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.is_museum = is_museum
    if self.is_museum is True:
      self.is_museum = "The Museum:"
      self.location = location
    if self.is_museum is False:
      self.is_museum = "The Collector"
      self.location = ""
  def sell_artwork(self, art, price):
    if self == art.owner:
      new_listing = Listing(art, price, self)
      veneer.add_listing(new_listing)
  def __repr__(self):
    client_title = "{} {}. {}".format(self.is_museum, self.name, self.location)
    return client_title
class Listing:
  def __init__(self, art, price, seller):
    self.art = art
    self.price = price
    self.seller = seller
  def __repr__(self):
    return print('The artwork "{}" is being sold for {}.'.format(self.art.title, self.price))

edytta = Client("Edytta Halprit", 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, edytta)
veneer = Marketplace()

the problem is here:

def __repr__(self):
    return print('The artwork "{}" is being sold for {}.'.format(self.art.title, self.price))

python expects __repr__ method to return a string, it returns None. We can deduce as much from the error:

TypeError: str returned non-string (type NoneType)

Thank you for replying.

How is it returning None? I don’t see how it can return two different times.

I also forgot to mention that the full outcome when running is:

The artwork “Girl with a Mandolin (Fanny Tellier)” is being sold for 6M (USD).
Traceback (most recent call last):
File “script.py”, line 57, in
veneer.show_listings() File “script.py”, line 20, in show_listings print(listing)
TypeError: str returned non-string (type NoneType)

here:

for listing in self.listings:

this will trigger the __repr__ method of Listing class, does that make sense?

However, this magic method doesn’t return a string, where python expects so. Which is what gives the error.

return keyword hands back data to the caller, and then execution of the function is complete. So unsure what you mean by two different times

I laughed when i understood.

My repr return states to print it out

return print('The artwork "{}" is being sold for {}.'.format(self.art.title, self.price))

Removing print solved it.

Thank you for explaining!

and print() “returns” None (actually, its the absence of a return value).

but python expected a string, but got None, thus the type mismatch and the error.

i was indeed hoping you got to the right conclusion yourself, of course i could have told you to get rid of print, but that wouldn’t achieve the same learning effect (i think)

Thank you for trying! I think I could of caught it after you stated it if i didn’t spend so much time trying to solve it myself. Calling a function and getting the repr to work has been a difficult task. The way you tried did help!

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.