Can Python 🐍 functions return multiple values?

Well, it's complicated...

Let me explain πŸ‘‡
Strictly speaking, the answer is NO, Python 🐍 functions cannot return multiple values...

But if they can't, then how can I explain this code πŸ‘‡
We can better understand what is going on if we import the module `dis` (as in DISassemble).

With the module `dis`, we can get a lower level view on what Python is doing under the hood πŸ‘‡

The LOAD_CONST line shows that the `42, 73, 0` I wrote is actually a tuple!
So, in essence, when we write a `return` statement with comma-separated values, we are actually bundling them into a _single_ return value:

A tuple.

That's why I can call the same function `foo` and assign the result to a single variable πŸ‘‡
This shows that, strictly speaking, Python functions only return single values; they only return single objects.

The fact that one object can be a collection of _other objects_ is a whole other story.

HOWEVER, Python 🐍 also supports unpacking:

mathspp.com/blog/pydonts/d…
That's the feature that let's us write multiple variables on the left of the function call πŸ‘‡

By returning a tuple and immediately using unpacking assignments, it _looks like_ Python 🐍 can return multiple values from a function.

Did it make sense?
I hope you got something out of this thread.

My name is Rodrigo and this is what I like to do: teaching Python 🐍

If you'd like daily Python content on your timeline, follow me @mathsppblog.
Remember:

Strictly speaking (from the Python 🐍 language point of view), functions cannot return multiple values.

When you return comma-separated values, you are actually returning ONE tuple.

However, by using unpacking, you can do the next best thing and mimic that behaviour.
This thread was very well-received, so I made a short YouTube video on this subject:

β€’ β€’ β€’

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

Keep Current with Rodrigo πŸπŸ“

Rodrigo πŸπŸ“ 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 @mathsppblog

Jan 4,
Today, I'm spending the day learning in public about socket programming (in Python 🐍).

My starting point is this:

β€œI think sockets are like tunnels/roads that allow different programs to talk to each other directly.”

This 🧡 will evolve as I learn and experiment πŸ‘‡
My idea is to experiment a bit with sockets in the REPL, then probably build a toy program that uses sockets to send random information through sockets.

I'm starting off with the β€œSocket Programming HOWTO” in the Python docs (docs.python.org/3/howto/socket…).

Let's go πŸš€
Alright, so, I'm not completely wrong in my intuitive understanding of sockets.
The HOWTO says

β€œOf the various forms of IPC, sockets are by far the most popular.”

IPC – Inter-Process Communication.

Turns out sockets are a good cross-platform IPC mechanism.
Read 30 tweets
Dec 31, 2021
Every day is a great day to start learning Python 🐍.

However, the new year is about to start.
Make good use of that extra motivation. πŸ’ͺ

Here is some of my best Python 🐍 content to help you out:
Want to learn about list comprehensions?

I have a comprehensive thread and a free cheatsheet for you.

(πŸ‘† see what I did there? My puns are fantastic!)

Here is the thread:

And here is the link to the free cheatsheet:

mathspp.gumroad.com/l/cheatsheet_l…
Read 12 tweets
Dec 29, 2021
Here is a countdown of my 5 best Python 🐍 articles of the year πŸ‘‡πŸ§΅

How many of them did you read?

mathspp.com/blog
Number 5.

There is my Pydon't article about Python 🐍's pass by value vs pass by reference vs pass by assignment behaviour.

πŸ”— mathspp.com/blog/pydonts/p…
Number 4.

In 4th place is an article that I published less than a month ago, so it's quite cool that it got so much visibility.

It's an article in which I analysis multiple solutions to a programming challenge from Advent of Code.

πŸ”— mathspp.com/blog/advent-of…
Read 7 tweets
Nov 25, 2021
I enjoy thinking about multiple possible solutions to the same Python 🐍 problems.

I do!

Call me crazy πŸ€ͺ, I don't care!

In the past, I've tweeted simple challenges πŸ†, you tweeted solutions, and I wrote a thread analysing your solutions.

Here is a meta 🧡 of past challenges!
The first challenge I ever posted was to implement the Caesar cipher

This is the thread with the solutions πŸ‘‡

The moral of the story was that very little people know about the `str.maketrans` and `str.translate` methods.

Read 21 tweets
Nov 25, 2021
Want to boost your Python 🐍 problem-solving skills?

I am looking for highly-motivated individuals that want to stay ahead of the curve!

If that's you, keep on reading πŸ‘‡...

Because tomorrow I'm launching the Python Problem-Solving Bootcamp!
This is a bootcamp focused on writing code.

Throughout the bootcamp you will solve 50 programming puzzles over the course of 25 days.

By going through the puzzles, you'll be practising your Python skills, and writing code is the best way to improve!

But there's more to it.
You will be getting 2 challenges/day.

Does that sound scary?

Good! If it does, then it's because it is an excellent growing opportunity.
Read 13 tweets
Nov 21, 2021
I have been tweeting about Python 🐍 string formatting.

I have been preaching πŸ™ that f-strings are the best string formatting alternative.

Then comes the string method `.format`.

And only then, %-formatting.

Here is a thread 🧡 comparing the 3 πŸ‘‡
In its most basic form,

πŸ‘‰ %-formatting uses % and a letter inside the string
πŸ‘‰ `.format` replaces sequences of {} with the data
πŸ‘‰ f-strings use {} to insert the data _inside_ the string

Here is how it looks like πŸ‘‡
An undervalued feature of string formatting is that you can easily determine whether your data should be formatted with its string (str) or with its representation (repr).

For debugging, `repr` is usually more useful.

Here is how this looks like πŸ‘‡
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

Too expensive? 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!

:(