2026-09-03 –, Auditorium
A generator, duck typing, and branchless conditionals walk into a bar…
Sounds like the start of a bad Python joke, but it’s actually a list of three features used in my favourite line of Python code.
You have a favourite line of code as well, right?
In this talk we’ll go over two quintessential features of Python programming (generators and duck typing) and you’ll learn how to use them effectively.
We’ll also look at branchless conditionals and understand how this unusual idea can shape the way you think about coding.
In the end, we’ll put the three together to write a powerful Python idiom and conclude I have terrible taste!
This talk will cover three major topics: generators, duck typing, and branchless conditionals. We devote roughly the same time for each topic.
First, we cover generators. We start by introducing the keyword yield as a “temporary return” and then see a couple of examples of generators, making clear the contrast between lazy and eager looping techniques.
We explain how generators have the potential to save time and computational resources and we also present generator expressions, as those will be important in the end.
Then, we talk about duck typing in Python and show examples of duck typing in our daily lives through an example inspired in biology and taxonomy. We also show how duck typing is present in standard Python code, for example when we think about the idea of iterable, to show it's not a weird concept that no one uses in practice.
When talking about duck typing we also make the connection to protocols and typing.Protocol, but this section is not focused on code since the main goal is to make sure everyone understands the idea behind duck typing.
Finally, we understand what a branchless conditional is, we see some examples of what branchless conditionals could look like in Python, and we try to understand whether the gain you get by writing less code outweighs the potential readability issues you may or may not have.
This is all done by using an example implementation of a text processing function and then refactoring it by using branchless conditionals.
In the end, we put all three concepts together in a single line of code to create a Python idiom. We use this piece of code to think about what it means to write “good” or “elegant” code and how to achieve it.
python fundamentals
Hi, I'm Rodrigo Girão Serrão from sunny Portugal 🇵🇹.
I'm a prolific Python author and speaker, with multiple books published independently and dozens of talks and tutorials given at the largest Python conferences in the world. I also blog frequently about Python and publish two Python newsletters: the weekly mathspp insider 🐍🚀 and the daily Python drops 🐍💧.
I have extensive experience teaching people from all walks of life – from kids in school, to professionals in various industries, to retirees – and there is a clear consensus that my students enjoy my clear examples, the live-coding during my lessons, and most surprisingly: my quirky sense of humour.
