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

ابزارهای یادگیری هوش مصنوعی را باز کنید

ثبت‌نام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک می‌کنند سریع‌تر از هر ویدیو یاد بگیرید.

توضیح صحنه شکارچی عبارات مرور فلش‌کارت تمرین تکرار مکالمه تعاملی
ثبت‌نام رایگان
انگلیسی 20:45 Science & Tech

10 Python Features You’re Not Using (But Really Should)

Late Night with Seth Meyers · 57,518 بازدید · اضافه شده 1 ماه پیش

زیرنویس‌ها (488 بخش‌ها)

00:00

Here's a CSV file with millions of sales

00:04

records. And here I have a function that

00:06

reads the file and computes the total

00:08

sales amount. Now, when I run this, you

00:10

see that every time I read this file, if

00:12

I need it in my code somewhere, it's

00:14

going to take time. I mean, it's not

00:16

that bad. This file actually reads like

00:18

10 million records. So, I'm kind of

00:20

surprised it works this fast. But every

00:22

time you do it, you actually have to

00:23

wait for the dis IO. This is what

00:25

happens when I add caching. As you can

00:28

see, the second read happens instantly.

00:30

And what's really cool is that this is

00:32

really easy to add in Python. In today's

00:35

video, I'll walk through 10 features

00:37

exactly like this. Really powerful and

00:40

really easy to add in Python. Now, if

00:43

you want to learn more about how to

00:44

design a piece of software from scratch,

00:46

I have a free guide for you. You can get

00:48

this at iron.codes/design

00:50

guide. contains seven steps I take when

00:53

I design new software and hopefully it

00:55

helps you avoid some of the mistakes

00:56

that I made in the past.

00:59

Guide the link is in the video

01:01

description. There's lots of features in

01:03

Python that help you reduce code size,

01:05

improve clarity, eliminate entire

01:07

classes of bugs, and speed up expensive

01:10

operations. And unfortunately, most

01:13

developers don't know they exist or

01:15

don't know when to apply them. And

01:17

because they're part of the standard

01:18

library, you actually don't need extra

01:20

dependencies of frameworks. Python is

01:23

really a batteries included language.

01:27

Now the first thing I already kind of

01:28

showed you is caching. So in the before

01:31

version of the code, we simply had this

01:33

total from file function. There's

01:35

nothing special here. Just uses pandas

01:37

to read a CSV file. But if you want to

01:40

cache this function, that's actually

01:41

really easy to do. So in this case you

01:44

simply import from funk tools the cache

01:49

decorator and then you just write cache

01:52

on top of the total from file function.

01:56

If I now run the code then you see that

01:58

this is what happens. So the second read

02:00

happens instantly. You also see that it

02:02

doesn't print reading the file anymore

02:04

because this is actually cached. So it

02:06

doesn't execute the function twice. So

02:08

this is really perfect for expensive

02:11

file parsing, large configuration

02:13

loading, uh calculations that are

02:15

expensive rendering templates or

02:18

anything deterministic. You eliminate

02:21

duplicated work and it's going to make

02:23

your system much more responsive. It's

02:25

great fit for uh CLI tools, ETL

02:28

processes, any repeated batch

02:31

operations. The second feature that I

02:33

use a lot is protocols. So if you take a

02:37

look at this piece of the script. So

02:39

what this does is that it takes a sale

02:41

object that's actually a very simple

02:43

class that just has an amount and a

02:45

currency and a converted value and then

02:48

it uses a rate fetcher to convert that

02:52

sale amount into a different currency.

02:54

Now the code contains these types of uh

02:57

type annotations which is fine. Actually

02:59

the Python interpreter ignores these at

03:02

runtime. So they're mainly there for you

03:04

as a developer to better understand the

03:06

code. The issue here is that the value

03:08

that's injected here is of type static

03:11

rate fetcher and that directly ties this

03:14

function to this particular class. So if

03:17

you wanted to replace this with some

03:19

other rate fetching method maybe you

03:21

want to take it from a database or you

03:23

want to use an external API. This is uh

03:26

annoying because then uh the type

03:28

doesn't match. You can't simply create

03:30

another class that has exactly this

03:32

structure and then put it in here. Well,

03:34

you can actually your Python code will

03:36

run, but you run into all sorts of uh

03:39

type issues while you're working on the

03:40

code. So, that's not so nice. What's

03:43

nice about protocols is that it allows

03:45

you to specify a sort of contract of

03:48

what an object is supposed to be like

زیرنویس کامل در پخش‌کننده ویدیو موجود است

با تمرین‌ها یاد بگیرید

تمرین‌های واژگان، گرامر و درک مطلب از این ویدیو بسازید

واژگان و گرامر آزمون درک مطلب آزمون IELTS تمرین نوشتاری
ثبت‌نام برای تمرین
هنوز نظری وجود ندارد. اولین نفری باشید که افکار خود را به اشتراک می‌گذارد!

ثبت‌نام کن و همه امکانات رو باز کن

پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن

شروع رایگان یادگیری زبان