I've seen different questions about casting while unpacking strucutures of the same type. I.e.
def foo():
return ('1', '2')
x, y = foo() # as str
x, y = map(int, foo()) # as int
x, y = [int(n) for n in foo()] # equivalently as int
But say you had
def foo():
return ('s', '1')
and you wanted to unpack and cast only the second element of the tuple. Of course there is
x, y = foo()
y = int(y)
which, in the name of simplicity, could definitely be the most pythonic approach. Out of curiosity though, is there an even more succinct way to accomplish the same thing on a single line?
If you use Python 3.8+, you can use assignment expression :=
def foo():
return ('s', '1')
x, y = (v:=foo())[0], int(v[1])
print(x, y)
Prints:
s 1