One of my favourite programming & Python analogies…

The Coffee Machine—Function analogy

Let's make some coffee…

Read on
👇🪡🧵

1/ coffee machine in action, m...
A function is like a coffee machine

1️⃣
It needs inputs to work (arguments):
— water
— electricity
— ground coffee beans (or pods if so inclined)

You can choose to put different blends of coffee into your machine (choice of what argument to pass to function)

2/
2️⃣
You press the "On" button—this is equivalent to calling the function.

You can almost see the similarity between the typical "On" button and the parentheses ( ) used to call a function in Python!

3/ On button icon on most device
3️⃣
When you call the function/press the "On" button on the coffee machine, the machine will "do stuff" inside.

If you just care about drinking your coffee, you don't care what's happening inside the machine…

4/
If you're an engineer building your own coffee machine, or you want to open up your coffee machine to fix it or—ahem—make improvements, then you _do_ care about what's happening inside the coffee machine.

5/
4️⃣
When the coffee machine does its thing, hot, liquid coffee will emerge from the machine.

You could say the coffee machine _returns_ liquid coffee.

but, there's one problem…

6/
5️⃣
The coffee flows directly into the drainage tray at the bottom of the machine and will likely overflow, making a mess on your kitchen bench…

7/
You called the function like this:

>>> make_coffee(
... electricity,
... water,
... my_favourite_blend,
... )

and therefore, the coffee _returned_ is not collected and stored in anything…

8/
Instead, you should put a coffee cup in the machine when you turn it on:

>>> cup = make_coffee(
... electricity,
... water,
... my_favourite_blend,
... )

9/
PS: if you want to look at this analogy from an OOP perspective, then `CoffeeMachine` might be the class and `.make_coffee()` would be a method of the class.

10/
If you enjoyed this thread you can:

— treat yourself by making a coffee (if you drink coffee and it's not too late in the day)
— follow me @s_gruppetta_ct for more Python-caffeine
— retweet the first tweet below so others can enjoy your favourite blend of coffee

11/
Enjoy…

#coffee #espresso cup of espresso coffee in a...

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Stephen Gruppetta

Stephen Gruppetta Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @s_gruppetta_ct

Aug 10
The difference between shallow and deep copy in Python…

…in pictures

Read the thread for captions to the pictures!

👇🪡🧵

1/ Visual representation of a ...Visual representation of a ...Visual representation of a ...
Start by creating a list of lists. Here, I'm creating a list containing two teams, each team is another list

>>> teams = [
... ["Stephen", "Mary"],
... ["Kate", "Trevor"],
... ]

2/
You're creating three lists when you do this.

`teams` doesn't "contain" the other two lists, it has references to them

3/ Visual representation of a ...
Read 12 tweets
Aug 10
You can't copy an immutable object, such as a tuple…

<mini-thread>

👇🪡🧵

1/ import copy  # Create a tup...
If you try using `copy.copy()` in a tuple, you get the same instance back

`copied_author is author` returns `True`

2/
This is not the case if you're using a mutable object, such as a list.

`copied_author is author` now returns `False`

3/ import copy  # Create a lis...
Read 4 tweets
Jul 28
Have you used `subplot_mosaic()` in Matplotlib yet?

Or are you a dinosaur like me and still used older functions? After all, `subplot_mosaic()` was only introduced in 2020 in version 3.3

Today, I decided to finally explore `subplot_mosaic()`

👇🪡🧵
@matplotlib

/1 Image
You probably use either the functional interface:
>>> plt.plot(...)

or the OOP interface:
>>> fig, ax = plt.subplots()
>>> ax.plot(...)

You can read more about these two styles here:

/2
Let's first create a subplot using `plt.subplots()`

You can use your own image instead of the one I'm using

<Note: code is in ALT text of image for this and all other code snippets>

/3 import matplotlib.pyplot as...
Read 23 tweets
Jul 21
Sometimes, all you need is a Named Tuple

Choosing the right data structure can be tricky. Should you create a class? Maybe a list or tuple? A dictionary? Or dataclass?

All have their own place, but sometimes, the oft forgotten named tuple is all you need

👇🪡🧵

1/
You want to store people's names and use them often in your program. You want the first name, middle name, and last name to be easily accessible.

A full class definition would be overkill if all you need is to access the data…

2/
A list or tuple would do nicely, but you'll need to use indices 0, 1, and 2 to refer to the various parts of the name.

This can get annoying when writing the code, makes the code less readable, and can lead to bugs.

3/
Read 16 tweets
Jul 21
• Python Turtle Weekend Challenge

Those who follow me know I'm a fan of using `turtle` beyond the basic (& rather boring) drawing of simple shapes

Here's a challenge for upcoming weekend

• How would you implement this typewriter app using the `turtle` module?

info below
👇
The program should type all letters and numbers on your keyboard as you press the keys on the keyboard, including the basic punctuation symbols.

Carriage return should also work, but no need to bother about backspace to correct your mistakes…
I'll post my version after the weekend…
Read 4 tweets
Jul 20
Dunder Method of the Day:
__contains__()

As the Athletics World Champs are running, let's look at a track & field-themed example

Read on…
👇🪡🧵

1/ Image
You define a couple of basic classes:

`Athlete` needs a first and last name and has the two string representations `__str__()` and `__repr__()` defined.

2/
`Event` has an event name and a list containing the athletes taking part in that event.

You also have an `add_athlete()` method which does what it says on the tin.

3/
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(