While looking for some answers in a package source code (colander to be specific) I stumbled upon a string that I cannot comprehend. Also my PyCharm frowns on it with 'statement seems to have no effect'.
Here's the code abstract:
...
for path in e.paths():
keyparts = []
msgs = []
for exc in path:
exc.msg and msgs.extend(exc.messages()) # <-- what is that?
keyname = exc._keyname()
keyname and keyparts.append(keyname) # <-- and that
errors['.'.join(keyparts)] = '; '.join(interpolate(msgs))
return errors
...
It seems to be extremely pythonic and I want to master it!
UPD. So, as I see it's not pythonic at all - readability is harmed for the sake of shorthand.
If keyname evaluates to False, the and
statement will return false immediately and not evaluate the second part. Otherwise, it will evaluate the second part (not that the return value matters in this case). So it's basically equivalent to:
if keyname:
keyparts.append(keyname)
I'm not sure that it's very pythonic though, since the the version I just suggested seem much more readable (to me personally, at least).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With