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)
AI 요약
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)
로그인하여 댓글 달기인터랙티브 모드
퀴즈
정답:
관련 영상
Android Developer Story: Pocket FM cuts 50% in development time with Gemini in Android Studio
The Ending Of Interstellar Finally Explained
Tesla Self-Driving vs 318 Curves
Google Play Console: Streamlining workflows, from testing to growth
#WeArePlay: Hugo, Maliyo Games - Nigeria
Late Night with Seth Meyers
퀴즈
정답:
영상을 보면서 퀴즈가 나타납니다
암기 팁
이 영상에서
무료로 언어 학습