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
ابزارهای یادگیری هوش مصنوعی را باز کنید
ثبتنام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک میکنند سریعتر از هر ویدیو یاد بگیرید.
10 Python Features You’re Not Using (But Really Should)
زیرنویسها (488 بخشها)
Here's a CSV file with millions of sales
records. And here I have a function that
reads the file and computes the total
sales amount. Now, when I run this, you
see that every time I read this file, if
I need it in my code somewhere, it's
going to take time. I mean, it's not
that bad. This file actually reads like
10 million records. So, I'm kind of
surprised it works this fast. But every
time you do it, you actually have to
wait for the dis IO. This is what
happens when I add caching. As you can
see, the second read happens instantly.
And what's really cool is that this is
really easy to add in Python. In today's
video, I'll walk through 10 features
exactly like this. Really powerful and
really easy to add in Python. Now, if
you want to learn more about how to
design a piece of software from scratch,
I have a free guide for you. You can get
this at iron.codes/design
guide. contains seven steps I take when
I design new software and hopefully it
helps you avoid some of the mistakes
that I made in the past.
Guide the link is in the video
description. There's lots of features in
Python that help you reduce code size,
improve clarity, eliminate entire
classes of bugs, and speed up expensive
operations. And unfortunately, most
developers don't know they exist or
don't know when to apply them. And
because they're part of the standard
library, you actually don't need extra
dependencies of frameworks. Python is
really a batteries included language.
Now the first thing I already kind of
showed you is caching. So in the before
version of the code, we simply had this
total from file function. There's
nothing special here. Just uses pandas
to read a CSV file. But if you want to
cache this function, that's actually
really easy to do. So in this case you
simply import from funk tools the cache
decorator and then you just write cache
on top of the total from file function.
If I now run the code then you see that
this is what happens. So the second read
happens instantly. You also see that it
doesn't print reading the file anymore
because this is actually cached. So it
doesn't execute the function twice. So
this is really perfect for expensive
file parsing, large configuration
loading, uh calculations that are
expensive rendering templates or
anything deterministic. You eliminate
duplicated work and it's going to make
your system much more responsive. It's
great fit for uh CLI tools, ETL
processes, any repeated batch
operations. The second feature that I
use a lot is protocols. So if you take a
look at this piece of the script. So
what this does is that it takes a sale
object that's actually a very simple
class that just has an amount and a
currency and a converted value and then
it uses a rate fetcher to convert that
sale amount into a different currency.
Now the code contains these types of uh
type annotations which is fine. Actually
the Python interpreter ignores these at
runtime. So they're mainly there for you
as a developer to better understand the
code. The issue here is that the value
that's injected here is of type static
rate fetcher and that directly ties this
function to this particular class. So if
you wanted to replace this with some
other rate fetching method maybe you
want to take it from a database or you
want to use an external API. This is uh
annoying because then uh the type
doesn't match. You can't simply create
another class that has exactly this
structure and then put it in here. Well,
you can actually your Python code will
run, but you run into all sorts of uh
type issues while you're working on the
code. So, that's not so nice. What's
nice about protocols is that it allows
you to specify a sort of contract of
what an object is supposed to be like
زیرنویس کامل در پخشکننده ویدیو موجود است
با تمرینها یاد بگیرید
تمرینهای واژگان، گرامر و درک مطلب از این ویدیو بسازید
نظرات (0)
برای نظر دادن وارد شویدثبتنام کن و همه امکانات رو باز کن
پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن
حالت تعاملی
آزمون
پاسخ صحیح:
ویدیوهای مرتبط
Late Night with Seth Meyers
آزمون
پاسخ صحیح:
آزمونها هنگام تماشای ویدیو ظاهر میشوند
راهنمای حفظ
از این ویدیو
شروع رایگان یادگیری زبان