[Tutor] Appropriate use of assignment expression

dn PyTutor at DancesWithMice.info
Sat Nov 20 15:05:28 EST 2021


On 21/11/2021 06.01, Alan Gauld via Tutor wrote:
> On 20/11/2021 14:33, Richard Damon wrote:
> 
>>> Personally, I've been burned so many times with assignments
>>> inside tests in C that I try my best to avoid it 
> 
>> One BIG thing to point out is that C leaves a lot of details about the 
>> order things happen to up to the implementation, so this sort of 
>> statement might not work there.
> 
> To be honest there are many features of Python and its
> implementation of the walrus assignment that are tighter
> than C so my fears are not really justified, it's just
> painful experience making me wary. (And to be fair C++
> has tightened up many of C's shortcomings in those
> areas too)
> 
>> Personally, I tend to like to decompose into clearly defined functions 
> 
> Exactly, and that's what I tend to do in places where
> many might use the walrus. It's more expressive, easier
> to debug and extend.
> 
> The walrus is one of those 'nice to have' things that is
> mainly used by those who think there are prizes for
> writing the shortest code. It's just not true.


There are some situations where the Walrus is invaluable. The 'top' of a
while-loop with some sort of incremental-control may be one. I coded an
assignment-expression yesterday, and was almost sad when refactoring
suggested 'a better way' that saw it removed. (sigh)


However, whilst some may regard 'short' as 'elegant', the earlier
(posts) comments about readability are inescapable.

On which theme, such comment applies equally to the list-comprehension
component of the construction (and other comprehensions generally).

All of the above criticisms are confirmed by the Python interpreter. In
the sense that if there is a problem, it will flag the entire line.
Beyond simple syntax failings, it is no more able to guide you to the
actual clause at-issue, than a human reader! Is it the assignment, the
for, the if, the slice, or the walrus?


As a gentle guide, many would suggest that learners 'construct'
one-liners in their 'full-fat code' version first. In which case, both
Python and a debugger will be more helpful and less frustrating (when
needed). Also, once the construct is tested/proven, one can?should then
ask the question: what do I gain by condensing (part of) this?


Yes, a more skilled pythonista might be able to 'think in
comprehensions' and that is fine for him/her. Whither the rest of the team?

People sometimes talk of 'code-smells' - eg certain constructions which
indicate a coder's lack of Python-technique, or a failure to appreciate
the use of a recognised (and time-tested) pattern. There is a similar
warning sign when a unit of code fails its tests, for some non-trivial
reason, and the author stubbornly refuses to 'unwind' a one-liner! Ego
vs efficacy? Even with complete cooperation, if that is in-fact the
source of the fault, one has to then ask (again): what did I (really)
gain by condensing this?


(counter-argument: yes there are applications where minute
performance-gains are necessary, and thus such skills, valuable)
-- 
Regards,
=dn


More information about the Tutor mailing list