Veneer exercise

Hi guys, hope you all are well,

I am doing the Veneer project and I am a bit lost, I’ve been following the video tutorial for check any mistake or difference but I can’t find it.

Exercise

class Art:
    def __init__(self, artist, title, year, medium, 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_name}, {location}".format(artist=self.artist, title=self.title, year=self.year, medium=self.medium, owner_name=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 item in self.listings:
            print(item)


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 self == artwork.owner:
            new_sell = Listing(artwork, price, self)
            veneer.add_listing(new_sell)


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

    def __repr__(self):
        return "{x}: {x}".format(self.art.title, self.price)


veneer = Marketplace()
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)", 1910, "oil on canvas", edytta)
print(girl_with_mandolin)

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

I get an AtributeError: ‘Marketplace’ object has no attribute ‘listings’

But I can’t see the problem

Wherever you have created listings is not being executed. Try throwing in a print statment in your constructor just for debugging, does it get printed? If not, why not?

2 Likes

What I can see is the problem comes from here:

class Marketplace:

    def __init(self):
        self.listings = []
        
    def add_listing(self, new_listing):
        print(self.listings)# Even printing it sais the same thing, Market has no listings object
        #self.listings.append(new_listing)

so it doesn’t go through here but I can’t see anything strange

So the error isn’t where you think it is. Your method add_listing only gets called under certain circumstances. Your constructor should operate whenver a new instance is created from this class. Does it?

Maybe compare the code you’ve written with a class you know does what it’s supposed to upon creation of new instances. What’s the difference?

1 Like

The constructor works fine, if instead of print(self.listings) I print a normal string, it work, but is the listings list that’s giving me troubles.

It looks like I can’t call the list listings in another methods, if I declare the list inside add_listing, it works perfect this method

You’re assuming the code before this performs exactly as you expect. Take it back a few steps and make sure that every line does exactly what you need it to do. Test each and every line in the order they’re called if necessary.

1 Like

Hi @djseku ,

@tgrtim is suggesting that you add a print statement within the method here:

class Marketplace:
    def __init(self):
        self.listings = []

Try it, then create an instance of Marketplace. Does the print statement execute?

1 Like

Right, I feel stupid now, I woke up this morning, I opened my laptop straight away, and I just saw the mistake,

missing __ next to the init, that’s why listings wasn’t creating properly.

Thanks guys for all your help!

2 Likes