[Python-ideas] Inline assignments using "given" clauses

Eric V. Smith eric at trueblade.com
Fri May 11 21:39:50 EDT 2018


On 5/11/18 5:12 PM, Angus Hollands wrote:

> *Readability:*
> A smaller point is that I don't feel that ":=" is very readable. If we
> had to use an operator, I think $= is better, but me reasoning for this
> is weak. I think it derives from my observation that ":=" is slow to
> distinguish from "=".

:= would prevent you from using assignment expressions inside f-strings, 
which could be argued is a good thing.

To demonstrate, and just for giggles, this works in 3.6, and appears to 
have the desired behavior:

--------------------------
class X:
     def __init__(self, value):
         self.value = value
     def __str__(self):
         return str(self.value)
     def __format__(self, fmt):
         assert fmt[0] == '='
         self.value = eval(fmt[1:])
         return ''

x = X(3)
print(x)
f'{x:=4}'   # Behold!
print(x)
--------------------------

Produces:
3
4

I kid, of course.

Eric


More information about the Python-ideas mailing list