A Gentle Introduction to Python Instances

Michael Wayne Smith
4 min readAug 5, 2023
python

I love writing python scripts to automate simple tasks.

Do I need a python script to generate daily baseball odds and statistics so I can avoid the last place albatross in my fantasy baseball league?

loser

You bet I do!

The problem is, since I’m a middle_aged_self_taught_programmer I am always wading through a sea of misplaced variables and funky functions on a shoddy raft of jupyter notebooks.

Friends — that’s no way to go through life.

Someday when I grow up and score my dream job of being a entry level software developer at the tender age of 65, three days away from retirement, I will be called upon to write code that other people can use in my half week tenure at AARP Software Solutions Inc.

That’s where classes come in.

Don’t know the scope get the nope

So in python when I assign a value to a name I get a variable.

Here’s a variable x:

x = 1

When I define a function I get … well … I get a function.

Well I actually get an object, but more on that later….

Here's the function you defined sir.

So far so good

Now when we are working with classes, the “variables” that a class returns are called instances.

The same way that a function outside of a class is still a function, but a function inside a class is called a method.

Confused yet?

My co-workers at AARP Software Solutions Inc sure as heck are, sooo, let’s look at at example.

I am working with an Point class example I used in an earlier article which you can find here.

A list of instances

So now I want to create a list of five points on the Cartesian plane.

[<__main__.Point at 0x7f44a02d3310>,
<__main__.Point at 0x7f44a02d3220>,
<__main__.Point at 0x7f44a02d34c0>,
<__main__.Point at 0x7f44a02d3610>,
<__main__.Point at 0x7f44a02d3580>]

Well that’s not what I wanted.

I forgot to add a print statement.

This is a point class where x = 1, y = 0
This is a point class where x = 3, y = 0
This is a point class where x = 2, y = 0
This is a point class where x = 5, y = 0
This is a point class where x = 4, y = 0

What if I want to sort these points?

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

Input In [5], in <cell line: 1>()
----> 1 for point in sorted(integer_list):
2 print(point)


TypeError: '<' not supported between instances of 'Point' and 'Point'

Well that didn’t work.

Jared, where the heck is the hiring manager at AARP Software Solutions Inc?

Because clearly the new hires can’t even script a simple whiteboard interview question.

whiteboard
Credit: Ali Paige Goldstein, HBO

What kind of ship are we running around here?!?!

We are asking python to sort instances in the same way that you would sort a variable.

Bad idea.

We could pass a key to the sorting method using a lambda function.

This is a point class where x = 1, y = 0
This is a point class where x = 2, y = 0
This is a point class where x = 3, y = 0
This is a point class where x = 4, y = 0
This is a point class where x = 5, y = 0

A better solution

Since at AARP Software Solutions Inc our motto is “Mediocrity is Key”, we are going to leave that print out alone, close the bug report and go grab a burger.

You coming Jared?

But since you are committed to the highest level of excellence Medium readers, we are going to use a class method of our Point class.

With ease like a breeze

Now let’s call our sorting function to sort our list of integers with our method rather than a lambda function.

This is a point class where x = 1, y = 0
This is a point class where x = 2, y = 0
This is a point class where x = 3, y = 0
This is a point class where x = 4, y = 0
This is a point class where x = 5, y = 0

Great job everybody!

Well we started the day lost on the sea of mediocrity, floating along on a raft of disjointed jupyter notebooks and misplaced variables.

Now we our writing classes!

Now AARP Software Solutions Inc stock valuation has gone up 10x for this 10x developer and everybody is a billionaire.

Way to rally late in the game guys!

The best way to support my writing is to -> ☕ Buy Me A Coffee. ☕

You can also support me by following on Medium.

Feel free to reach out on LinkedIn, Twitter, or Facebook.

--

--