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

Brendan Barnwell brenbarn at brenbarn.net
Fri May 11 14:13:53 EDT 2018

On 2018-05-11 11:08, Brendan Barnwell wrote:
> . . . and it's true the latter is a bit more verbose in that case for
> little extra benefit.  But when the locally-defined value is used within
> a more complicated expression (like the quadratic formula example), I
> think readability goes down significantly.  To appease Tim, instead of
> using the quadratic formula, though, I will use a more realistic example
> that comes up fairly often for me: wanting to do some kind of
> normalization on a piece of data for a comparison, while keeping the
> unnormalized data for use within the block:
> 	if some_condition and (stuff:=
> get_user_input()).lower().strip().replace('-', ''):
> versus
> 	if some_condition and stuff.lower().strip().replace('-', '') given
> stuff = get_user_input():

	Ironically I weakened my argument by forgetting to finish my expression 
there.  I intended that chain of method calls to be used in a comparison 
to make the surrounding expression more complex.  So revise the above to

  	if some_condition and (stuff := 
get_user_input()).lower().strip().replace('-', '') == existing_value:


	if some_condition and stuff.lower().strip().replace('-', '') == 
existing_value given stuff = get_user_input():

Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown

More information about the Python-ideas mailing list