Email or username:

Password:

Forgot your password?
Sam Schlinkert

surprised to find that one of the things I miss most from Rust that's apparently not in Python is ability to assign a new variable directly with a proper (multiline-if-necessary) if/else statement. It nicely ensures that the variable will have a value after it's done, right?

In Rust:
let bar = if foo { "something" } else { "something else" };

4 comments
Craig Maloney ☕

@schlink Yeah, unfortunately I've done the following in python way too often:

bar = 'something else'
if foo:
bar = 'something'

Ellie

@craigmaloney @schlink Hang on, I think Python does do what you want:

bar = 'something' if foo else 'something else'
Eugen Rochko

@noelle @craigmaloney @schlink Don't they all have ternary operators? condition ? when-true : when-false

Ellie

@Gargron @craigmaloney @schlink This is Python's equivalent of the ternary operator, I think.

Go Up