Flexible, in-line variable assignment

python

Solution

In Python 3.8+, you can use assignment expressions (operator `:=`):

if (x := 3) == 5:
    print("that's odd")

y = (x := 3) + 10

Problem

I'd like to flexibly assign a value to a variable in Python, no matter where in my code that variable is. For instance, given a variable `x` in an if statement: ``` if(x == 5): print "that's odd." else: print "Woot." ``` I'd like to be able to assign `x` right in the if statement like this: ``` if((x=3) == 5): print "that's odd." else: print "Woot." ``` Is that possible? Here's another example. Let's say I have a line that's: ``` y = x + 10 ``` I'd like to assign x right there: ``` y = (x=3) + 10 ``` So I'm looking for a way to find a variable anywhere in my code and give it a value assignment. Is there a pythonic syntax for that?

Original source