Multiple Assignments in Python

preview_player
Показать описание
Chain those equals signs!

Python allows multiple assignments, or chained assignments, to assign multiple variables or expressions at once. This can be a useful tool but it is also a source of confusion when the multiple assignments involve the same name multiple times or when the assignment target is mutable. The assignments are evaluated right hand side first, then left to right, assigning each expression to the target in turn. This differs from many other programming languages and it is also different from the way Pythons assignment expressions (the walrus operator (:=) colon equals) works.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Laura M, Jameson, John Martin, Vahnekie, Pieter G, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:49 Multiple assignment example
2:12 Tricky example
4:54 Assignment expressions
6:56 Quiz time
Рекомендации по теме
Комментарии
Автор

"Let's talk about this video's sponsor.
Me. That's right. Me. I'm sponsoring my own video."

priceless. my guy said that with a straight face.

axeskull
Автор

tmp = ([1, 2, 3], 2)

Assignment from left to right
a = tmp[0]
b = tmp[1]
Array is assigned by reference not a copy. Changes to a will affect tmp.
We assign a reference of the tuple to a[b] which creates a circular reference.
a = [1, 2, (...)] where (...) = tmp
tmp = ([1, 2, (...)], 2)

Final assignment of RHS (tmp) to a
a = tmp
tmp = ([1, 2, (...)], 2)

kklowd
Автор

Love these obscure python videos that aren’t anywhere else on YouTube.

justinfuruness
Автор

I like that you're finally "supporting" your own channel / promoting your website more prominetely. I feel like it could be a very useful resource for some people but they just don't see it in the description.

memespdf
Автор

"... trick your friends or whatever, but in real code please don't ever do this."
This had me laughing out loud

heitortremor
Автор

The actual trick question is "how much did the sponsor pay you?"
Great Videos btw.

arbaazshafiq
Автор

Been coding in python for half a decade now ... how in the world did I not know about recursive data structures?

adityasriram
Автор

b = 2, a = 'Shub-Niggurath the Black Goat of the Woods with a Thousand Young', but I'm not sure about the 2.

sharkinahat
Автор

@3:14 “But, remember, before this line _a_ was _equal to_ this element.” This is clearly an understatement in this context, since here _a_ actually *was* (a name for) this element. :)

PureVirtual
Автор

I can think of a few examples of where "you really should never be writing code like this" actually leads to remarkable results if you understand how to abuse it. The doom fast inverse square root code is a gem of an example.

DavesSpace
Автор

this stuff seems really useful to make an effectively annoying python obfuscator

also, amazing sponsor!

fini
Автор

Exactly dude!!!
I am learning python from 2 months and just a week ago when I was experimenting with python I tried multiple assignments and I was just amazed that it didn't show any error and it worked and I liked it!!!
Glad that you made a Detailed video about this. This will be a great help ❤️👍

HypnosisBear
Автор

I have always found multiple assignments weird in Python. To me it only makes sense if assignments would be expressions, which I know they aren't. This also heavily tripped me up when I was once playing around with it a bit, because I fully expected them to start from the right instead of left.

It honestly don't know why this is the case and seems like more of a "quirk" than a "feature".

vinno
Автор

I hope your sponsor pays you well. You did a great job promoting them!

phelpsvdb
Автор

Welcome to a new episode of: "how to make code as complicated as possible" ;)

dennishmn
Автор

Oh crap, I liked the video before it even starting...

And that ad was great... 🙂👍🏽

You'll soon go viral for this sponsorship of yours... 😁

rahuls
Автор

Hands down the best python channel on youtube, don't stop making internet great again body

MrShika
Автор

i got a = ([1, 2, a], 2) and b = 2 (then plug "a" into itself recursively)

qschroed
Автор

The self-sponsor bit was hilarious.
Again, a video where I thought I knew what there was to know, but actually ended up not. I assumed it would work as it would in other languages, but nope.
I didn't even know that python had this cyclic business with lists containing themselves.
And I got the quiz example wrong!
More subtle than I would have expected.
My excuse is that I've only dabbled in Python :P

Vaaaaadim
Автор

Wow!!! I have never seen the [...] thing before in my life. I just watched this video yesterday. I came into work today and started to debug some code my boss wrote and found that it was printing out [...] I couldn't believe it. Because of this video, I knew exactly what the issue was and was able to fix it. It probably would have taken me an hour to find the bug otherwise. You sir have earned my like and subscribe.

makaipost