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
ابزارهای یادگیری هوش مصنوعی را باز کنید
ثبتنام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک میکنند سریعتر از هر ویدیو یاد بگیرید.
You’re Passing Way Too Many Arguments (and How to Fix It)
خلاصه هوش مصنوعی
This video provides a practical guide to refactoring messy, argument-heavy functions using the **Context Object pattern**. If you find yourself passing the same multiple parameters through various functions, this tutorial demonstrates how to group related data—such as database sessions, user IDs, and API keys—into a single object. By following a step-by-step refactoring process using Python’s `dataclasses`, you will learn how to clean up your code, reduce coupling, and simplify function signatures. The video also discusses real-world applications of this pattern and offers insights into when to use it versus when to avoid it.
آمار یادگیری
سطح CEFR
کل کلمات
کلمات منحصر به فرد
سختی
زیرنویسها (463 بخشها)
Do you ever feel like you're just
passing the same five arguments through
every function in your code? You could
use a singleton to try to fix that, but
that's not really recommended. But
there's another way to solve that, and
that's with a pattern called the context
object. I'm going to start today with a
simple example and improve it step by
step and introduce this pattern. Along
the way, I'll also talk about when not
to use this pattern and what kind of
features we have available in Python to
avoid too much coupling in our code. The
context object appears all over packages
and libraries. A very good example of a
context object is typically in the back
end, you're going to have a request
object that's going to contain the HTML
uh data or JSON data that's been sent,
header, stuff like that. So this is a
good example of a context object that's
used quite a lot in the back end.
Another example where you might see it
is if you have some template rendering
engine like Django or something. It's
going to have a context that contains
all the variables and the data that you
need in order to render that particular
template. Here's the example we're going
to work with today. As you can see,
there is a render article function right
here that gets a user ID, article ID, a
database session, a logger, an API key,
and maybe in the future even more
things. Then there's a bunch of code
here. There's some uh check if uh the
user is allowed to uh do this. We're
getting an article. If it's not there,
there's an article not found result. And
then finally if everything works out as
we want then we can actually render the
HTML and return that as a result. Then I
have a main function that's uh sets up
some logging. It initialize the database
creates a session has an API key and it
then calls render article to render some
of those articles. Now this doesn't use
an actual database in the cloud or
something. This uses um SQL alchemy
together with a SQLite in memory
database for just for demo purposes. And
this is the code for that. So there's an
article table. There's a function to
initialize the database. And this also
fills our in-memory database with a few
articles. So we can just write some code
around that. And then here's a function
that gives us a database session using a
generator and context manager. So
nothing too fancy, but at least allows
us to write some code that kind of
resembles what you might encounter in
real life. So let me run this file and
then this is what we get as a result. So
it renders the first article and the
second one basically there's
unauthorized access by user 41 and
that's because there is this hard-coded
check right here. But if I change this
to user 42, then actually this is an
article that is not found because the
there is no article with this ID in our
inmemory database. So then we get this
as a result. By the way, 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 it at
iron.co/designguide.
This contains the seven steps I take
whenever I design new software.
Hopefully it helps you avoid some of the
mistakes I made in the past.
Iron.co/designg codes/design guides link
is in the video description. Now the
problem we need to solve is that the
render article has a lot of different
things that you need to pass as an
argument. Now you might be able to split
up this function a bit more. You know
perhaps uh we could do the authorization
check somewhere else or maybe we could
pass the article as an argument uh to
زیرنویس کامل در پخشکننده ویدیو موجود است
واژگان کلیدی (48)
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 make someone or something different or to become different. It can also mean to stop using one thing and start using another, such as putting on different clothes.
To move past a person or place, or to hand something to another person. It also means to be successful in an exam, test, or course.
با تمرینها یاد بگیرید
تمرینهای واژگان، گرامر و درک مطلب از این ویدیو بسازید
نظرات (0)
برای نظر دادن وارد شویدثبتنام کن و همه امکانات رو باز کن
پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن
حالت تعاملی
آزمون
پاسخ صحیح:
آزمون
پاسخ صحیح:
آزمونها هنگام تماشای ویدیو ظاهر میشوند
راهنمای حفظ
از این ویدیو
شروع رایگان یادگیری زبان