Question in the "Veneer" Project in Python

So I had no particular issues with this project, but I did have one question. In the .sell_artwork() and .buy_artwork() methods inside the Client class, we are told to add the listing using veneer.add_listing(), or remove it with veneer.remove_listing(). I did that and it worked, but I was wondering how come we can call the veneer Marketplace without defining it anywhere in said class. Shouldn’t it be a TypeError ?

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:
      artwork = Listing(artwork.title, price, artwork.owner)
      veneer.add_listing(artwork)
  def buy_artwork(self, artwork):
    if artwork.owner != self:
      for listing in veneer.listings:
        if listing.art == artwork.title:
          artwork.owner = self
          veneer.remove_listing(listing)
    else:
      pass

Hello, @eduardoarrspidegonzl, and welcome to the Codecademy Forums!

It is a good idea to provide a link to a project or exercise, when it is the subject of a question or discussion.

Link to project: Veneer

In the global scope of your code, do you have a statement similar to the following?:

veneer = Marketplace()

If so, then veneer is a global name after the statement has been executed. Subsequently, that name will be recognized within functions that refer to it when you call those functions.

Hello appylpye and thank you for the fast answer! Next time I’ll provide the link, sorry about that.

Yes, I do have that line of code, but I instantiated it after I defined all the classes. When I called the buy and sell methods it was after the veneer instantiation, which makes sense, but my question is why isn’t it an error when I define the methods.

Thank you again!

Eduardo

1 Like

If I’m understanding your question correctly… This is covered in the Python documentation related to Python’s execution model.

1 Like

As long as that name is defined within the enclosing global scope by the time the method executes, that name is recognized, and is available for access. The name does not need to be defined before the method is defined.

Thank you both ! :))

1 Like