Python's Walrus Operator??

preview_player
Показать описание

Background Music:
Creative Commons / Attribution 3.0 Unported License (CC BY 3.0)
Рекомендации по теме
Комментарии
Автор

An obvious note is that the Walrus Operator only appeared since Python 3.8, so if you have to work on older systems, it will not be backwards compatible...

xonxt
Автор

I love that you added the "obvious" method which makes for a great explanation of when to choose what, i.e. readability. Great tutoring. Thank you! :=

finnthirud
Автор

wow, this was an easy and straight to the point explanation, thank you!

foreverunsure
Автор

Something worth noting about the walrus operator, is that it always "returns" the value of the variable, unlike a regular assignment operation (eg: x = 10), this is why it works inside stuff like an if statement or inside other operations.

In python, you have assignments, and expressions, you can assign an expression to something, but you cannot perform a normal assignment inside an expression because a normal assignment doesnt return anything.

AetherBreaker
Автор

Another nice example from the PEP with while:

while chunk := file.read(8192):
process(chunk)

You can't rewrite that without having to change the control flow.

nicktreleaven
Автор

Historical note from an old-timer:
We never thought to call it "the walrus operator" but . . .
Going back DECADES the := operator was used in Algol and derived languages (like Pascal) . . . for assignment statements.

davidking
Автор

I really like these style of `if` predicates and did not know about this operator.
Thanks

christosmantas
Автор

This is interesting to know and I appreciate how such syntactic sugars are useful. Also, I've encountered many situations where the use of such specific or unique syntax make reading the code more effort-full, which especially in teams and with team communication can slow development. Personally, if it can be done in a way which is faster to comprehend without major impacts to other aspects of the development cycle or to product maintenance, then that way is preferable.

Also, I cannot get the similarity of a C-syntax variable assignment inside a control structure's condition evaluation out of my head.

rask
Автор

Anybody else remembers this as the standard assign statement from Pascal?

stefaanverstraeten
Автор

exactly what i was looking for, thanks

turglow
Автор

My god I've been missing out on this one, thank you very much

scaffus
Автор

I like what i saw on your channel so far.
I suggest video about lambda functions. Especially in list comprehintion.
For me, at was always something strange, what i forget every time i learn it.

lirsanplay
Автор

nice way to explain is such a easy manner

AdityaSharma-wgrj
Автор

It can also avoid indentation. Before:
match1 = pattern1.match(data)
if match1:
result = match1.group(1)
else:
match2 = pattern2.match(data)
if match2:
result = match2.group(2)
else:
result = None
After:
if match1 := pattern1.match(data):
result = match1.group(1)
elif match2 := pattern2.match(data):
result = match2.group(2)
else:
result = None

Example from the PEP.

nicktreleaven
Автор

I used to do this a lot in C, but forgot that it exists in Python so thanks for reminding me.

GuagoFruit
Автор

Can someone tell me why my VS code doesn't show the result directly but show extra details of the folder and stuff before giving the output ?

basharat
Автор

Seeing as you had to make a video to explain what it does I would argue assigning to the variable is more easily understood. Good video.

cleverclover
Автор

I didn't know something like this was in Python. Very interesting. This is very similar to the "=" operator in C, except that can be used for assignment anywhere in an expression, including something that looks like Python's assignment statements. The problem with that is it's quite easy to accidentally turn the equality operator "==" into the assignment operator, especially in conditions, where it will cause an assignment and make the condition succeed when it shouldn't. It's good practice to use parentheses to make deliberate embedded assignments clear, and some compilers can detect if you don't. But I like this approach, of requiring you to write a different operator if you really mean to do this.

fllthdcrb
Автор

Dude I'm a freshman taking APSCP, our teacher is teaching VIA python. I've picked up on it fast, but a lot of advanced stuff like this is hard for me (Especially since we're working on AP task). I really appreciate the tricks and tips you provide!

tinypus
Автор

fascinating, I have wondered if something like this exists in python, since it does in e.g. Swift and theoretically Rust

NowLP