Taking Your Python Skills to the Next Level With Pythonic Code – Introduction
This is the introduction to a series covering Pythonic code written by Michael Kennedy of Talk Python To Me. Be sure to catch the whole series with 5 powerful Pythonic concepts and over 45 minutes of video examples.
WHAT IS PYTHONIC CODE?
When developers are new to Python, they often hear this phrase Pythonic and they ask what exactly does that mean?
In any language, there is a way of doing things naturally and another way that fights the conventions of the language. When you work naturally with the language features and the runtime features, this is called idiomatic code, and in Python when you write idiomatic Python we call this Pythonic code.
Pythonic code is code that matches the idioms and expectations of Python developers and the CPython runtime.
One of the challenges in Python is it’s super easy to get started and learn the basics. You can start writing programs before you really master the language.
What that often means is people come in from other languages like C++, Java, or something like that, is they will take algorithms or code that they have and bring it over to Python and just tweak the syntax until it executes in Python. Let’s make this concrete and assume you’re coming from Java.
In Python there is often a more concise, more natural way of doing things. When you look at code that came over from Java, we’ll just take a really simple example – if you have a class you might have a GetValue() and SetValue() method, because in Java that is typically the way you do encapsulation. People bring those classes and code over and migrate it to Python. They might still have this weird getter / setter type of code. That would look completely bizarre in Python because we have properties.
So in this case, getter / setter methods are non-pythonic. Properties solve the same problem in a pythonic way.
WHAT YOU WILL LEARN IN THIS SERIES
In this blog post series, we are going to look at this idea of Pythonic code. It’s pretty easy to understand but it turns out to be fairly hard to make concrete. You’ll see a lot of blog posts and things of people trying to put structure or examples behind this concept of Pythonic code.
We are going to go through 5 examples of things I consider Pythonic. By the end, you’ll have some examples, patterns, and so on to give you a solid grip on what Pythonic code is.
WHY DOES PYTHONIC CODE MATTER?
When you write Pythonic code, you are leveraging the experience of 25 years of many thousands, maybe millions, of developers. These guys and girls have worked in this language day in and day out for the last 25 years and they perfected the way of working with classes, functions, loops, and more in Python.
Especially when you are new, it’s very helpful to just study what those folks have done and mimic that. When you write Pythonic code, you are writing code that is
- specifically tuned to the CPython runtime
- easily read and understood by Python developers
- you are often writing code that is simpler and cleaner
- if you are working on an open source project, it will be easier for other contributors to join
- if you are working on a software team, it’s easier to on-board new Python developers into your team
THE FIVE PYTHONIC EXAMPLES
- Stop using lists for everything
- Hacking Python’s memory with __slots__
- Merging Dictionaries
- Processing large data sets with yield and generators
- Lambda expressions
If you’re ready to learn about Pythonic code, in-depth, by example, continue on to the first post: #1 Stop using lists for everything.
Michael Kennedy
‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.
I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.
I’m friendly, so feel free to say hello!