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.

Scene Explainer Phrase Hunter Flashcard Review Shadowing Practice Talk Back
Sign Up Free
English 21:56 Science & Tech

SOLID: Writing Better Python Without Overengineering

Late Night with Seth Meyers · 62,729 views · Added 1 month ago

Subtitles (532 segments)

00:00

This video is brought to you by

00:02

Squarespace. You may have heard of the

00:04

solid design principles. In fact, you

00:06

may have heard of them from an old video

00:08

done by me. Robert Martin, aka Uncle

00:11

Bob, came up with these principles in

00:13

the early 2000s to help engineers write

00:16

better software. But are they still

00:19

relevant? And do they actually help in

00:21

Python or do they make things worse? To

00:24

test that, I'm going to refactor a piece

00:26

of Python code using these principles.

00:28

and we're going to see what we end up

00:30

with. And along the way, we're going to

00:32

figure out what actually improves the

00:35

code. So, here's the example. I have a

00:37

CSV file with sales data. There's

00:40

customer names, addresses, items, uh,

00:43

dates, price, taxes, basically your

00:47

standard sales data. Some of these rows

00:50

are actually returns. So David Pon for

00:52

example from Christensen Land he ordered

00:56

a monitor but sent it back and got back

00:58

his money and his taxes and that's

01:00

indicated by a negative price. Now what

01:03

we want to do is generate JSON reports

01:05

that includes some useful information

01:08

like the number of unique customers in a

01:10

certain time period, the average order

01:12

value, percentage of returns, the total

01:15

sales, and being able to generate that

01:17

report for any date range that we want.

01:20

So here's a first stab at this in

01:23

Python. I have a class called messy

01:26

sales report. So that's already

01:28

indicates something, right? And it has a

01:30

generate method that gets an input file,

01:32

gets an output file, a start date, and

01:34

an end date, which is the period that we

01:36

want the report to be of. And then this

01:38

is the code. We read the CSV file, we

01:40

apply the filter, uh, we check the

01:42

average order returns, the total sales,

01:45

and then we create the report, we dump

01:47

it into a file, and we're done. So when

01:51

I run this then we now have a file here

01:53

called sales report that actually

01:55

created a report for 2024. There were 33

01:59

customers. This was the average order

02:01

value. The returns percentage and the

02:04

total sales in this period was this

02:06

amount. Now of course because we have

02:08

this start date and end date. I can also

02:11

do this from uh only for the second part

02:14

of 2024.

02:16

And then when I run this again and I

02:18

look back at the sales report now you

02:20

see we actually have 20 customers. There

02:22

are no returns and we have of course a

02:25

lower number of total sales. So that's

02:28

our messy report. Now you can already

02:31

see that there's plenty of problems with

02:33

this code. It's like a big mess. Thus

02:36

the class name. So what does that to do

02:38

with design principles? Well, design

02:40

principles help you tackle this kind of

02:43

code and make it better. And a very

02:45

well-known set of design principle is

02:47

solid. What does that actually mean?

02:49

Well, each letter stands for a specific

02:52

principle. The S stands for single

02:56

responsibility and that means that each

02:59

part of your code, each class, each

03:01

function should do one thing. Then you

03:04

have open closed the O in solid. That

03:06

means that code should be open for

03:08

extension but closed for modification.

03:11

In this example, it should be easy to

03:14

add another thing to this report,

03:16

another metric. So open for extension,

03:19

but it should not be easy to completely

03:21

modify the structure of the report. Then

03:23

you also have the L that stands for list

03:26

of substitution. And that means that

03:28

subtypes should behave like their base

03:31

types. So if you have a superass that

03:34

specifies some sort of method, then you

03:36

don't want subasses to interpret

03:39

arguments of that method completely

03:41

differently cuz then it's going to be

03:43

very hard to swap out a subtype for

03:46

another subtype. The I stands for

03:48

interface segregation. That means you

03:51

should prefer small focused interfaces

03:54

over large ones. So don't create huge

03:58

abstract base classes or protocol class

04:00

with hundreds of methods in them but

04:02

keep them relatively small and focused

04:04

on what you actually need it for. Then

04:06

we have the D which stands for

04:08

dependency inversion. That means that

04:10

highle code shouldn't depend on lowlevel

04:13

details. Now in Python you typically

04:15

don't follow these like commandments.

04:17

They're also very much based in

04:18

objectoriented design and well, Python

04:22

supports object-oriented programming,

Full subtitles available in the video player

Practice with Exercises

Generate vocabulary, grammar, and comprehension exercises from this video

Vocabulary & Grammar Comprehension Quiz IELTS Exam Writing Practice
Sign up to practice

Comments (0)

Login to Comment
No comments yet. Be the first to share your thoughts!

Sign up to unlock full features

Track progress, save vocabulary, and practice exercises

Start learning languages for free