The goal? Being able to replace any loop with comprehensions. Why? By talking non-stop about all the features of comprehension and the walrus operator, I’ll (easily!) convince you that, by mastering some Python aspects to the extent of abusing them, you’ll improve in your daily coding.Abstract
The structure of the talk will be based on taking increasingly large snippets of normal-looking code and translating them into not-so-normal comprehension monsters.
And I promise you it’s a good idea. As an example, that’ll give us the chance to cover:
E.g.: do you know the difference between
[[x+y for y in range(10)] for x in range(10)] and
[x+y for y in range(10) for x in range(10)]?
E.g.: do you know what happens if you remove the
sum([x for x in range(10)])? Does it still work?
E.g.: are the following lines correct? If not, how could you fix them?
function(function := print)
assert ((value := 5) if value else 3) == 5
print(*[value := x for x in range(5)], sep=str(value))
E.g.: would you be able to re-implement all built-in dictionary operations as one-liners?
andcan be used in expressions
andwhy they can confuse you
ormake the code clear!
E.g.: what’s the value of
a after executing the following line?
((a := 5) and (a := 0) or (a := a + 1)) and ((a := a + 1) and (a - 2) and (a := 5))
And so on. Yes, all of these are examples constructed to be intentionally obscure; but being able to see through them will be very useful as soon as you’ll meet some unintentionally obscure legacy code.TagsClean Code