The Secret Python Operator No One Talks About

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

Рекомендации по теме
Комментарии
Автор

I've never understood what's the point of writing this kind of esoteric syntax to avoid a very basic, fast to read and easy to understand control structure, unless it's for performance gains.

Naegimaggu
Автор

The magic operator here isn't the walrus, it's "or". Return primary if primary evaluates to True, else return backup. This "or" is what eliminates the separate assignment

harelsegev
Автор

Thank you!
I didn't know it. Thanks for sharing this outstanding Python operator.😊

subhankarsamanta
Автор

Man! C#'s coalesce operator (?? or ??=) seems so much cleaner.

keneola
Автор

funny how this doesn't need a new operator in other languages but python decided to be different

lebenet
Автор

once you know learn the walrus, this does make more sense. a lot more. it is not two separate ideas, but one. the variable we set and the way it is handled are shown together.

dalistrata
Автор

"if user := (primary or backup or None):" to make it the same as the original solution

wartem
Автор

This wasnt the best example. Lets say you have a function that checks if your string input is an intager. If it is, return the number, if it isnt, return false. Now, you can write:
if n := checkInt(s):
print(n)
else:
print("It's not an int")

Instead of:
n = checkInt(s)
if n:
...

I have used this operator a lot while working with sockets (when i want to use a recieved message only if it exists), but I'm sure you can find some use for it😊

nesicvojin
Автор

Usually i write it as bool

User = primary or secondary

josephvictory
Автор

I really like the name because its just so memorable lol

udbhavshrivastava
Автор

And how is this better? Unless your doing some only this many lines to write this program challenge

srepinS
Автор

Please stop checking for null as if the value you're checking is a boolean

FabioGamingFG
Автор

I like Ben, suprised by the collab lol.

trietang
Автор

Didn't Guido Van Rossum leave the python foundation over this?

bobDotJS
Автор

Does that mean "" will also work as a user?
Also, doesn't other PL already have something like this, too?

asagiai
Автор

Is it like ternary operator on other languages?

ajhaykumar.n
Автор

Would have been better if there was a ternary operator

Germisstucklmao
Автор

does this not work
user = user if user else primary

warguy
Автор

can't just do this?

if user in {primary, backup}:
print ("success.")

else:
print("failure.")

terabhaininja
Автор

People getting mad they cant understand simple logic

JohnDoe-vod