The Python Walrus Operator - New in Python 3.8

The Python Walrus Operator - New in Python 3.8

Tech With Tim

4 года назад

126,577 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Tech With Tim
Tech With Tim - 09.01.2020 17:06

What do you guys think of this new feature? Can you see yourself using it? Let me know !!

Ответить
wlqpqpqlqmwnhssisjw
wlqpqpqlqmwnhssisjw - 12.08.2022 21:19

thanks ,please explain python 3.10 updates too

Ответить
Akari 玉 Amano
Akari 玉 Amano - 26.06.2022 21:49

cool video

Ответить
Gamer In Your Town
Gamer In Your Town - 24.08.2021 18:28

does anyone explain better than him()?

Ответить
Rishabh Semwal
Rishabh Semwal - 30.06.2021 18:11

It would be better if you had run all the codes. Very bad tutorial. Walrus is already unreadable and your tutorial makes it more difficult to understand by not providing the output

Ответить
Hans Bezemer
Hans Bezemer - 13.06.2021 15:48

They should have made it into a proper function SET(a,b) where B is an expression, A is a lvalue (variable) and SET a function which returns B. It would have been a whole lot clearer and properly wouldn't have caused that much controversy. It's a function. It's not an operator like +, -, etc. since what does 1 := 5 mean? What does it return? 5?

Added bonus:
a = SET(b, 5+3) or in C a = b = 5+3;

Ответить
Cleris du Christ
Cleris du Christ - 23.09.2020 18:11

import this

Ответить
TRENWAR
TRENWAR - 18.09.2020 09:41

I think the walrus operator isn't too beginner-friendly but yeah you can use it if you want and i think w'all must use it

Ответить
Shaharyar Ahmed
Shaharyar Ahmed - 10.09.2020 16:27

I don't understand why people are spreading hate towards the walrus operator! Its an amazing operator which makes our lives easier. Sometimes this world is to hard to comprehend

Ответить
Odis Clemons
Odis Clemons - 23.08.2020 04:19

I'm not a Python dev but i'm trying to understand why this is a big deal. Is this like the await keyword in JS?

Ответить
Juxarius
Juxarius - 09.08.2020 16:37

Late to the party here but I dont understand why people are angry over this feature, i think its really elegant and so many times i wished i could do what C or C++ could in Python and now i can without repeating myself awkwardly

Ответить
spam colector
spam colector - 23.04.2020 09:49

Does variable of walrus operator is removed after loop?

Ответить
Ali S
Ali S - 17.02.2020 18:32

how do install that version of python???

Ответить
JOEY!
JOEY! - 13.02.2020 10:43

We need egg man operators...

Ответить
Joshua Evans
Joshua Evans - 04.02.2020 19:38

I can see why this could be controversial, but I kind of dig it. I can definitely see the usefulness

Ответить
AvidCluelessGuy
AvidCluelessGuy - 03.02.2020 04:25

I feel like there’s a random pascal fan at Python HQ. walrus was originally used as all-purpose assignment in the advent of high level programming languages. C essentially reversed that trend by replacing it with just “=“, and marking the ‘equals to binary operator’ be “==“. A part of me wonders if they’re adding walrus to add specific precedence, but that would imply to me there’s some brick wall with interpreting python code (with inline assignment), so they added a new operator. Maybe readability is the long term goal? Something’s off about the decision to add this.

Ответить
Glen Neff
Glen Neff - 02.02.2020 07:58

You're highlighting the silliest feature of 3.8. If you want to look at something truly impactful to Python, check out multiprocessing.shared_memory.

Ответить
Random Hominid
Random Hominid - 30.01.2020 07:21

Personally, I love it. It makes things less clumsy.

Ответить
Tim Scongack
Tim Scongack - 30.01.2020 02:51

Walrus? Why not Zoidburg? :=

Ответить
thok ling
thok ling - 27.01.2020 04:53

Using a Pascal operator in a C based language to help avoid confusing programmers with complicated things like syntax and logic. It seems like increasing the confusion instead. Kudos, Python.

Ответить
BankOfTrust NWOBOT
BankOfTrust NWOBOT - 25.01.2020 23:11

Bad programmers think about syntax. Good programmers think about data structures.

Ответить
Siqi Zhang
Siqi Zhang - 25.01.2020 20:54

STUPID

Ответить
SkoolyRatt
SkoolyRatt - 25.01.2020 16:47

i got a python 7.8 inches in my pants.... once you get laid youll forget all about this python bs

Ответить
MooGoo
MooGoo - 24.01.2020 20:55

I. Love it.
practical, pythonic, elegant solution, easy to read.
gonna move up to 3.8.1 then

Ответить
hazcoper
hazcoper - 22.01.2020 15:48

What about speed? Is it faster?

Ответить
GAMARAYBURST
GAMARAYBURST - 22.01.2020 10:06

Looks interesting

Ответить
lolepopie
lolepopie - 21.01.2020 17:28

lol why though

Ответить
Philip
Philip - 21.01.2020 11:02

Less lines of code doesn’t always make it more readable. It might be worth it in the case where you would have the same action before and inside the while loop, but aside from that you’re probably better off doing it the normal way.

Ответить
yuri
yuri - 21.01.2020 03:14

So syntactical sugar which may be neat but definitely is less readable.

Ответить
ahah86
ahah86 - 19.01.2020 02:23

if there is not actual difference in terms of performance and optimization, I guess is still preferable to use the more readable solution, right?

Ответить
Marko Jozic
Marko Jozic - 18.01.2020 17:43

so basically assignments return the value of the assignment .. just like in C :-)
i don't really know why this is not default-behavior

Ответить
Reddemon815
Reddemon815 - 18.01.2020 17:10

Can you talk about the PEP recommendations for walrus operators? Feel like a lot of the criticism is due to readability.

Ответить
Большаков Василий
Большаков Василий - 18.01.2020 16:56

long lines ahead

Ответить
Sportsquirrel
Sportsquirrel - 18.01.2020 05:39

It probably doesn't work this way, but I would probably use it more if the variable was local only to the indented part.

Ответить
Dominic Richens
Dominic Richens - 17.01.2020 05:05

Great. In go := is "Short variable declaration". Gonna make switch between Go and Python even more confusing ...

Ответить
Paŭlo Ebermann
Paŭlo Ebermann - 17.01.2020 02:56

So now we have a mixture of = in assignment statements and := in assignment expressions (which are a bit less powerful). And other uses of = for non-assignments (like named parameters).

C-based languages have just one syntax for assignments (=), as expressions which also can be used in statement positions. But that was not possible in Python, as = was used in different contexts already.

Ответить
JayRizzo
JayRizzo - 17.01.2020 02:28

Is there a performance compromise and using the new walrus functionality?

Ответить
ashrasmun
ashrasmun - 16.01.2020 22:51

I am confused why is it controversial. I've been using such construct in C++ for quite a while:

if (auto *ptr = some_factory) do_stuff;

and I never had problems with readability. But I guess there are still people who would write:

If (statement)
return true;
else
return false;

Instead of:

return statement;

Ответить
Blackasthesky
Blackasthesky - 16.01.2020 00:53

Really cool feature! I actually immediately had ideas where i could have used it in the past few weeks

Ответить
Stian Andreassen
Stian Andreassen - 16.01.2020 00:36

I argued for just evaluating the operations first before compare, that is if (n = len(a)) > 10 would be valid. The choice of := is rather messy. I might use it, but I really don't like the syntax souce to avoid ambiguity.

Ответить
alicE mysterY
alicE mysterY - 15.01.2020 20:12

I am new to python and code. So, I could see myself using the walrus. Koo koo katchoo

Ответить
Oliver Thane
Oliver Thane - 15.01.2020 13:08

This looks really useful in list, etc comprehension too for testing/returning a value in the filter section etc

Ответить
Raquel Foster
Raquel Foster - 15.01.2020 05:46

The while expression always felt broken in Python without the ability to assign a value in the condition. The walrus is a little silly because you could just as easily enforce putting parentheses around the assignment which makes it pretty obvious what you're doing, but ... maybe that just felt wrong to people because I guess requiring parenthesis is sorta non-Pythonic unless you're using tuples. Whatever. I think they just like to make Python quirky.

Ответить
Z Assassin
Z Assassin - 15.01.2020 03:16

Unisys MCP Algol has had this operator since forever, I first used it in 1983.

Ответить
Rick Hornsby
Rick Hornsby - 15.01.2020 02:23

It’s interesting, and I can see why I might find it controversial - the code is shorter, and more compact but maybe at the cost of being slightly less readable. There’s almost a reverse Polish notation sense with the parens and how the line is read by a human.

However - I run into this situation often enough where I need to a) evaluate an expression b) test the expression result against a condition and c) use the expression result in subsequent code. I hate writing even mildly complex expressions more than once, so I often will relegate it to a new function so that b and c call foo() to get the value - DRY. That still means the operation has to run twice, or I have to implement some kind of variable cache (in the rare case the op is expensive).

I’ll probably find uses for it, sparingly. I hate unreadable code more than repetitive code. The latter can be refactored, the former is a nightmare to decipher.

Ответить
TheNumberOneD
TheNumberOneD - 15.01.2020 02:03

your 3rd example is bad, in the no walrus example you set ans to the input value, while in the walrus example you set it a boolean.

Ответить
The Walrus
The Walrus - 15.01.2020 01:49

I’m not sure if I approve of this…

Ответить
RHS
RHS - 14.01.2020 18:40

Visual Basic has this operator

Ответить
Chris Hubley
Chris Hubley - 14.01.2020 16:06

Similar to lambda functions, I find code written like this difficult to read. Sure, there are use-cases, but I certainly wouldn't default to sprinkling walrus operations throughout my code.

Ответить