Python 🐍 objects have one important characteristic about them:

Some objects are mutable, which means their contents can be changed.

This ⚑🧡 will explain what (im)mutability means in Python 🐍.

Let's go πŸš€
The (im)mutability of an object depends on its type.

In other words, (im)mutability is a characteristic of types, not of specific objects!

For example,

πŸ‘‰ ALL integers are immutable.
πŸ‘‰ ALL lists are mutable.

With me so far?
But what exactly does it mean for an object to be mutable?

Or for an object to be immutable?

Recall that an object is characterised by

πŸ‘‰ its identity
πŸ‘‰ its type
πŸ‘‰ its contents.

I wrote about that in this thread πŸ‘‡

Take a type, any type.

If you can change the contents of its objects without changing its identity and its type, that type is mutable.

Lists are a great example of a mutable data type.

Why?
Because lists are containers: you can put things inside lists and you can remove stuff from inside those same lists.

πŸ‘‡ you can see how the contents of the list `obj` change as we make method calls...

But the identity of the list remains the same!
However, when dealing with immutable objects, it's a completely different story.

If we check an English dictionary, this is what we get for the definition of β€œimmutable”:

πŸ‘‰ adjective: immutable – unchanging over time or unable to be changed.
Immutable objects' contents never change.

Take a string as an example.

Strings are a good example for this discussion because, sometimes, they can look mutable.

But they are not!

Here πŸ‘‡, string indices cannot be assigned to!
If you want to change the string that `s` points to...

You need to assign a completely new string to `s`!

Another thing that hints at the fact that strings are immutable are its methods.

String methods all return new strings.

None of them change the string in-place...
Notice how `s` wasn't updated automatically to "HELLO, WORLD!".

Instead, the new string was created and returned to you.

As you can see, you can use a string to build OTHER strings!

But you can never change the original string itself.
As a reference, here are some common (im)mutable built-in types:

πŸ‘‰ MUTABLE: lists, dicts, and sets
(containers are typically mutable)

πŸ‘‰ IMMUTABLE: ints, floats, complex numbers, Booleans, strings, and tuples.
That's it for this ⚑🧡

Did you learn something new?

The content for this thread come from the subsection of my most recent Python 🐍 article, you can check it out here πŸ‘‡

mathspp.com/blog/pydonts/p…
Are you learning Python 🐍?

Then, follow @mathsppblog for πŸ”₯🀯 Python content!

TL;DR

πŸ‘‰ mutable objects are those whose internal contents can be changed (like lists)

πŸ‘‰ immutable objects can only be used to create new ones (list strings or ints)

See you around πŸ‘‹
Woooops, the second bullet point has a typo: "list" β†’ "like"

πŸ‘‰ immutable objects can only be used to create new ones (like strings or ints)

Thanks @learn_byexample for noticing & letting me know.

β€’ β€’ β€’

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

6 Nov
The Python 🐍 Standard Library is one of the reasons I love πŸ’™ Python.

πŸ“‚πŸ” dealing with your filesystem is super simple.

All you have to do is use the `pathlib` module.

This short thread is a mini `pathlib` cookbook 🍳, showing some example usages of `pathlib`.

Ready πŸš€? Image
πŸ“‚ Creating a `Path` object

`Path` objects are the bread and butter of `pathlib`.

Here, I just create a path with no arguments, which defaults to the path `.`

Notice how I used `Path` but I get a `WindowsPath` back.

`pathlib` automatically detects your OS πŸ˜‰ Image
πŸ“‚ Getting the parent

The `parent` attribute returns the logical parent of the path you have at hands πŸ‘‡ Image
Read 11 tweets
5 Nov
πŸ—“οΈ the weekend is coming πŸ₯³

If you want, use it to rest! You deserve it!

Or, make this a memorable weekend:

Let this be the weekend you implement your first neural network 🧠 from scratch, in Python 🐍

Here's the roadmap πŸ›£οΈ

πŸ‘‡πŸ§΅ Image
By the time you are done, you'll have

πŸ‘‰ a minimal neural network framework
πŸ‘‰ solved a ML problem with accuracy > 90%

And all this with a surprisingly short amount of code!
First, we start with an appetizer.

It will spark your interest, and acquaint you with some terminology.

Your appetizer is this amazing video by @3blue1brown πŸ‘‡

Read 9 tweets
5 Nov
I gave you a Python 🐍 challenge πŸ†...

You delivered πŸ’ͺ!

This thread 🧡 will review some of your `argmax` implementations.

I'll also tell you what's good βœ… and bad ❌ about them.

I'll also tell you which one I think is the best, most Pythonic ✨ one.

Let's go πŸš€
The `argmax` function returns the index of the maximum element of the argument.

So, why don't we write this solution?

Compute the maximum, and then use `.index` to get its index! πŸ‘‡

But there are two issues here...

Can you see them? πŸ‘€
πŸ‘‰ first issue is that it traverses the argument twice;
(once to get the max, one more time to find its index)

πŸ‘‰ second issue is that it assumes the argument has a method `.index`, which might not be the case.

However, let's try and fix the first problem first...
Read 20 tweets
1 Nov
Here is a Python 🐍 function that tells you if the input number is even or odd...

But how does it work? πŸ‘‡

It looks a bit weird, doesn't it?

Someone DM'd me this function from a @realpython article, and now this ⚑🧡 will break it down for you.

Here we go πŸš€
If you have heard of Boolean short-circuiting, that's the short answer:

Boolean short-circuiting is what's responsible for the behaviour of `f`.

Let me explain...

First, we need to understand the precedence of the `and` and `or`.

Let me add parentheses πŸ‘‡
`and` has higher precedence than `or`.

In other words, `and` β€œbinds tighter” to its operands than `or`.

Now, usually we use `and` and `or` with Boolean operands, to return a Boolean value.

However, `and`/`or` work with arbitrary objects as operands.

We call it,
Read 14 tweets
31 Oct
Would you like me to teach you how to write Python 🐍 code that is more elegant, more effective, and more expressive than what you have been writing until now... for free?

I am looking to teach Python to people who want to master Python to the best of their abilities.
Do you? If you do, then I know I can help you improve your skills.

How do I know that?

My book has over 10,000 readers, and all of them have one thing in common: they are humans.
Python has dozens of built-ins, and all of them have multiple usage patterns. No human knows all of them by heart, but my research bundled them up nicely in a book for you to reference and learn from.
Read 12 tweets
30 Oct
πŸπŸ”¦ Python under the hood.

πŸ‘‡πŸ§΅ Here's a thread on the dunder method `__init__`.

You can't say you **know** Python if you don't know this, so...

In this thread we will cover the dunder method `__init__` and its role in Python OOP.

Here we go πŸš€
β€œinit” is short for β€œinitialise” or β€œinitialisation”.

That, right there, tells you what `__init__` is for!

When you create a new instance of a class, you use the method `__init__` to initialise it:

In other words, you use `__init__` to customise the instance.
Here's an example for you.

Say you want to create a class that represents people.

People have names, right? And whenever you create a new person, you want to give them their name.

It will be the __init__'s job, then, to take that name and associate it with the person object.
Read 15 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 Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(