The video owner has disabled playback on external websites.
This video is no longer available on YouTube.
This video cannot be played right now.
Watch on YouTube
Unlock AI-Powered Learning Tools
Sign up to access powerful tools that help you learn faster from every video.
Why Your Code Isn’t Pythonic (And How to Fix It)
AI Summary
In this video, learners will discover how to transform "unpythonic" code into clean, idiomatic Python through a step-by-step refactoring of a fitness tracker script. You will learn the core principles of "Pythonic" design—prioritizing readability and simplicity—by replacing unnecessary classes with efficient functions and implementing **context managers** for safer file handling. Additionally, the tutorial demonstrates how to use **type annotations** to improve code clarity and predictability. By the end, you will gain practical skills in following the "Zen of Python," helping you write code that is not only functional but professional and easy to maintain.
Learning Stats
CEFR Level
Total Words
Unique Words
Difficulty
Subtitles (628 segments)
The code that you're seeing here works,
but it's ugly, repetitive, for both,
totally unpythonic.
Today, I'm going to take this pretty
clunky fitness tracker script and
refactor it step by step into clean,
beautiful, idiomatic Python. Before I
start refactoring that code, let's talk
about this word Pythonic. It's one of
those terms that lots of Python
developers throw around, but what does
it actually mean? Is it some kind of
secret club that only a few gurus are a
part of? Or do you need to buy a bunch
of iron gold merch like the hoodie that
I'm wearing right now or this nice hat
or this mug that's now available? Of
course not.
There's no clear definition. But to me,
Pythonic code is code that uses Python's
strengths, like readability, simplicity,
for example, in many case using
functions over classes and
expressiveness, using features of Python
that are really powerful like
comprehensions or generators. And it
should feel natural in Python. Well,
that's not very specific, obviously. Now
the closest thing you we might have to a
definition is the Zen of Python. If you
write import this in a Python script or
in the command line interface like here,
you get the Zen of Python. And this
outputs a number of guiding principles
like uh beautiful is better than ugly.
Don't know exactly what that means.
That's kind of up to you what you find
beautiful or what you don't find
beautiful, right? To me, the favorite
one is simple is better than complex.
And that doesn't just apply to Python,
but to any programming work. And in
fact, most anything that you do in life.
But in this video, when I talk about
Pythonic, I mean that I'm going to be
using idiomatic syntax, things that fit
well with Python. I'll try to avoid
overengineering and overall make the
code just easier to reason about. Now,
with that in mind, let's take a look at
the original version of the code. The
starting point is this fitness tracker
class that has a log food method to log
some food items. So this writes to a
foodc file. We have a log activity
method as well that logs to activities
CSV. And we have some method that
prepares some sort of summary for the
day. And then it retrieves information
from this CSV file and from this one as
well. And then it's going to print out
some sort of summary. Some issues in
this code are the manual file opening
and closing, no type annotations,
there's no data classes or something, no
error handling, pretty verbose uh
control flow, especially in run day
summary. And the question is also what
the added value of the class here is. So
there's lots of things wrong with this
code. I mean it works. When you run
this, it creates these CSV files and it
prints out a summary. And this is what
these files look like. Pretty basic
obviously, but clearly there's a lot to
improve in this code. So the first thing
that I'm going to do to make this more
Pythonic is to replace that fitness
tracker class or actually just remove it
and turn these methods into functions.
Because as you can see, the class has no
member variables. There's no reason for
this to be a class. We don't need
multiple instances of a fitness tracker.
So we can simply turn this into
functions. So that's actually very easy
to do. I just remove this. And then I
de-indent this part
like so. And we will remove the self
from all of these argument list because
obviously we don't need those anymore.
Like so. Now the nice thing is at the
bottom of the script, we don't need to
create an object anymore. So we don't
need that object and we can simply call
the functions directly. So we're logging
the food, we're logging activity and
we're running the day summary like so.
And now I'm going to run the code and as
you can see it does exactly the same
thing and it creates the files again.
But now we went from having this class
to having just a couple of simple
functions. And typically I much prefer
this kind of setup. Actually, overall I
typically start with functions and I
only add classes when I feel like they
add some value. On top of that, if you
want to write tests for this, that's
also a bit easier because you don't need
to create an object. Next step is that I
want to take a look at how we're dealing
with files. Now, you can do it this way
in Python. You can open files and close
files, but uh this is actually pretty
dangerous if something goes wrong. For
example, let's say that for some reason
you're not able to open a file or here
in the runday summary if you call that
before you actually log anything then
well these files don't exist. Now there
is a check here. I'll come to that later
but there's all sorts of things that can
go wrong here and then if it goes wrong
we don't properly close the file and in
terms of resource management that's not
Full subtitles available in the video player
Key Vocabulary (50)
Used to refer to the person or people that the speaker is addressing. It is the second-person pronoun used for both singular and plural subjects and objects.
To move quickly on foot by taking steps faster than a walk, where both feet are momentarily off the ground. It can also mean to lead, manage, or be in charge of an organization or activity.
To bring something new into existence or to make something happen using your imagination or skills. It involves producing something that did not exist before, such as a work of art, a solution, or a digital file.
Practice with Exercises
Generate vocabulary, grammar, and comprehension exercises from this video
Comments (0)
Login to CommentSign up to unlock full features
Track progress, save vocabulary, and practice exercises
Interactive Mode
Quiz
Correct answer:
Related Videos
Compose Preview | Jetpack Compose Tips
Reverse Engineering Ali Express Spy Camera
Android Developer Story: Entri cut UI development time by 40% with Gemini in Android Studio
I Made a Classic Refactoring Mistake
Google Play Console: Streamlining workflows, from testing to growth
Late Night with Seth Meyers
Quiz
Correct answer:
Quizzes appear as you watch the video
Memory Tip
From this video
Start learning languages for free