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

AI学習ツールを解放

登録して、すべての動画からより速く学べる強力なツールにアクセスしましょう。

シーン解説 フレーズハンター カードで復習 シャドーイング練習 トークバック
無料で登録
A2 初級 英語 26:39 4,260 単語 Science & Tech

Why Your Code Isn’t Pythonic (And How to Fix It)

Late Night with Seth Meyers · 147,262 回視聴 · 追加日 2ヶ月前

AI要約

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.

学習統計

A2

CEFRレベル

4,260

総単語数

668

ユニークな単語

3/10

難易度

語彙の多様性 16%

字幕 (628 セグメント)

00:00

The code that you're seeing here works,

00:02

but it's ugly, repetitive, for both,

00:06

totally unpythonic.

00:08

Today, I'm going to take this pretty

00:10

clunky fitness tracker script and

00:12

refactor it step by step into clean,

00:15

beautiful, idiomatic Python. Before I

00:18

start refactoring that code, let's talk

00:20

about this word Pythonic. It's one of

00:23

those terms that lots of Python

00:25

developers throw around, but what does

00:27

it actually mean? Is it some kind of

00:30

secret club that only a few gurus are a

00:33

part of? Or do you need to buy a bunch

00:35

of iron gold merch like the hoodie that

00:38

I'm wearing right now or this nice hat

00:42

or this mug that's now available? Of

00:45

course not.

00:47

There's no clear definition. But to me,

00:50

Pythonic code is code that uses Python's

00:53

strengths, like readability, simplicity,

00:57

for example, in many case using

00:59

functions over classes and

01:01

expressiveness, using features of Python

01:04

that are really powerful like

01:05

comprehensions or generators. And it

01:08

should feel natural in Python. Well,

01:11

that's not very specific, obviously. Now

01:14

the closest thing you we might have to a

01:17

definition is the Zen of Python. If you

01:20

write import this in a Python script or

01:24

in the command line interface like here,

01:26

you get the Zen of Python. And this

01:28

outputs a number of guiding principles

01:31

like uh beautiful is better than ugly.

01:34

Don't know exactly what that means.

01:36

That's kind of up to you what you find

01:38

beautiful or what you don't find

01:39

beautiful, right? To me, the favorite

01:41

one is simple is better than complex.

01:44

And that doesn't just apply to Python,

01:45

but to any programming work. And in

01:48

fact, most anything that you do in life.

01:51

But in this video, when I talk about

01:53

Pythonic, I mean that I'm going to be

01:55

using idiomatic syntax, things that fit

01:58

well with Python. I'll try to avoid

02:00

overengineering and overall make the

02:02

code just easier to reason about. Now,

02:04

with that in mind, let's take a look at

02:06

the original version of the code. The

02:09

starting point is this fitness tracker

02:11

class that has a log food method to log

02:14

some food items. So this writes to a

02:17

foodc file. We have a log activity

02:20

method as well that logs to activities

02:23

CSV. And we have some method that

02:27

prepares some sort of summary for the

02:29

day. And then it retrieves information

02:32

from this CSV file and from this one as

02:34

well. And then it's going to print out

02:36

some sort of summary. Some issues in

02:39

this code are the manual file opening

02:41

and closing, no type annotations,

02:44

there's no data classes or something, no

02:47

error handling, pretty verbose uh

02:50

control flow, especially in run day

02:52

summary. And the question is also what

02:54

the added value of the class here is. So

02:57

there's lots of things wrong with this

02:59

code. I mean it works. When you run

03:01

this, it creates these CSV files and it

03:04

prints out a summary. And this is what

03:07

these files look like. Pretty basic

03:09

obviously, but clearly there's a lot to

03:11

improve in this code. So the first thing

03:14

that I'm going to do to make this more

03:17

Pythonic is to replace that fitness

03:20

tracker class or actually just remove it

03:22

and turn these methods into functions.

03:25

Because as you can see, the class has no

03:27

member variables. There's no reason for

03:29

this to be a class. We don't need

03:31

multiple instances of a fitness tracker.

03:34

So we can simply turn this into

03:36

functions. So that's actually very easy

03:38

to do. I just remove this. And then I

03:40

de-indent this part

03:43

like so. And we will remove the self

03:47

from all of these argument list because

03:49

obviously we don't need those anymore.

03:52

Like so. Now the nice thing is at the

03:54

bottom of the script, we don't need to

03:55

create an object anymore. So we don't

03:58

need that object and we can simply call

04:01

the functions directly. So we're logging

04:03

the food, we're logging activity and

04:04

we're running the day summary like so.

04:07

And now I'm going to run the code and as

04:09

you can see it does exactly the same

04:11

thing and it creates the files again.

04:13

But now we went from having this class

04:15

to having just a couple of simple

04:17

functions. And typically I much prefer

04:20

this kind of setup. Actually, overall I

04:22

typically start with functions and I

04:24

only add classes when I feel like they

04:26

add some value. On top of that, if you

04:28

want to write tests for this, that's

04:30

also a bit easier because you don't need

04:31

to create an object. Next step is that I

04:34

want to take a look at how we're dealing

04:35

with files. Now, you can do it this way

04:38

in Python. You can open files and close

04:41

files, but uh this is actually pretty

04:43

dangerous if something goes wrong. For

04:45

example, let's say that for some reason

04:47

you're not able to open a file or here

04:50

in the runday summary if you call that

04:52

before you actually log anything then

04:54

well these files don't exist. Now there

04:56

is a check here. I'll come to that later

04:59

but there's all sorts of things that can

05:01

go wrong here and then if it goes wrong

05:03

we don't properly close the file and in

05:05

terms of resource management that's not

完全な字幕は動画プレーヤーで利用可能

重要な語彙 (50)

you A1 pronoun

あなたは親切です。(You are kind.)

run A1 verb

走る: 彼は毎朝公園を走っています。経営する: 彼女は小さなカフェを経営しています。

create A1 verb

「作る」や「創造する」という意味で、新しいものを生み出すことを指します。画家は美しい絵を創造します。

練習問題に挑戦

この動画から語彙・文法・読解の練習問題を作成

語彙と文法 読解クイズ IELTS試験 ライティング練習
登録して練習
まだコメントがありません。最初に考えをシェアしましょう!

登録してすべての機能を解放しよう

進捗を追跡、単語を保存、演習で練習しよう

無料で語学を始める