Python Classes & The Market

Let’s Code the Free Market

I wanted to go over classes in python and how to inherit their information so that we have an intermingling program of classes working with proper functions. My scenario here is a market place to sell art, and the clients who buy and sell.

So to begin I’ll set up a Marketplace class. Now my market place is going to be simple, as it’s just really a listing of what’s for sale. To begin it’ll be an empty listing, and as our market place business matures pieces of art can be added, or removed. So I’d like to add the methods that will show me the listing, add, and remove them.

Marketplace Class

That takes care of my market, now I’ll need the Client class. I know there are a lot of private collectors, and the second type are mostly museums. I’ll set up my Client class to automatically assume an entry to be a private citizen, but can also accept museums as well. I’ll also set up a repr function to tell me the status of our client (name, location, is_museum). For my convenience I’ll also set a default value for location and is_museum.

Client Class

Now I’ll need a class for the works of Art. I’ll want to keep track of each piece. For this class I’ll want to know the artist, title, medium, year, and who owns it. Pretty straight forward, and a repr function to give me all the details.

Alright, this is a good start. Let me create an instance of my market place, and two clients. One private (Edytta), and another a museum (MOMA). I’ll have these two sell to each other later on. I’ll also create an art instance owned by Edytta.

Everything works. My Art class gave back the details for girl_with_mandolin, and my two clients came back. You can see I passed Edytta as the last parameter for my girl_with_mandolin instance, so printing out girl_with_mandolin shows me that she’s the owner of that piece of art. My market place is set up. Now I need to add more functionality, but first I’ll also need a class for the Listing of what’s available. I’ll use this in a future method to sell artwork from the Client Class, and pass it to the Market place Class where it has a list of listings. Think of it as an intermediary passed from my function up to the Marketplace class.

You can see in the repr function I parsed the art parameter down to artist, and title. If I had just done the information it would have spat out too long of a string, and I just want the artist, and title specifically.

So let’s create a sell_artwork method. This will be in the Client Class, as the client should be the only one able to sell of course. My sell_artwork method will take in the artwork, and the asking price. For safety I’ll want to make sure the client is the owner of the artwork so I’ll use a conditional checking if the artwork parameter passed in is equal to the Client’s class of self. They should be the same. I’ll then create a new_listing and set it to the class Listing (my intermediary) to properly format it, and then send it up to the Marketplace class of Sothebys, so it can be added to the empty array of listings set up earlier.

From here I can now have the edytta instance use the sell_artwork function. Let’s give it a try, and use soethbys’ instance to run the show_listing function to see if Edytta’s entry was registered.

Presto! Awfully nice when a plan comes together. Now I need a client to have the ability to buy. The MOMA museum is chomping at the bits to get their hands on the girl_with_mandolin. 6M is a steep price, but thankfully the world of NYC museums are full of donors with suspicious funds. Someone’s bound to find 6M in the cushions.

My Client class will need a buy_artwork function. The only parameter needed will be the artwork. This function will require the changing of ownership, and changing the listing to remove the bought artwork, so some function juggling will be required.

I often talk about function juggling. It’s important to have a clear idea of what is affected when programming, so always make sure to take a little time to understand the structures you are building. It does become easier over time, and this is a fairly simple example so let’s dive in.

First I’ll make sure the buyer isn’t the owner of the artwork and use the not equal operator (!=) on the artwork.owner and the self. After that I’ll begin looking for the item. I’ll set up an empty variable (art_listing) to only be filled if the artwork is found in the market place’s listings. I’ll iterate through Sotheby’s listing to see if anything matches the artwork passed in as the parameter. If it’s found BAM I’ll assign it to my empty variable. For efficiency I’ll add in a break statement, because the list could be very long.

Outside of my iteration if the art_listing variable is filled I’ll assign the owner to self (the object that called the function), and remove it from the market place’s listing.

Super, after running the functions I can see the girl_with_mandolin changed owners.

From here I can add much more functionality like a wallet for my clients to keep track of their budget, and a wish list for pieces of art. These sorts of practices are excellent for getting really familiar with the principles of Object Oriented Programming. Keep coding coders, and think about future projects you may want to personally do. I’m looking to implement a football fantasy script once the new season starts. Friends beware, I’m finally going to win with the power of python!

I’m a web developer, and data scientist by hobby. Yes, it can be a hobby. I blog about all things code.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store