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

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

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

توضیح صحنه شکارچی عبارات مرور فلش‌کارت تمرین تکرار مکالمه تعاملی
ثبت‌نام رایگان
A2 مقدماتی انگلیسی 19:45 3,134 لغت Science & Tech

You’re Passing Way Too Many Arguments (and How to Fix It)

Late Night with Seth Meyers · 118,701 بازدید · اضافه شده 3 روز پیش

خلاصه هوش مصنوعی

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.

آمار یادگیری

A2

سطح CEFR

3,134

کل کلمات

530

کلمات منحصر به فرد

3/10

سختی

تنوع واژگان 17%

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

00:00

Do you ever feel like you're just

00:01

passing the same five arguments through

00:03

every function in your code? You could

00:06

use a singleton to try to fix that, but

00:08

that's not really recommended. But

00:10

there's another way to solve that, and

00:12

that's with a pattern called the context

00:14

object. I'm going to start today with a

00:17

simple example and improve it step by

00:19

step and introduce this pattern. Along

00:21

the way, I'll also talk about when not

00:23

to use this pattern and what kind of

00:25

features we have available in Python to

00:28

avoid too much coupling in our code. The

00:30

context object appears all over packages

00:33

and libraries. A very good example of a

00:37

context object is typically in the back

00:39

end, you're going to have a request

00:41

object that's going to contain the HTML

00:44

uh data or JSON data that's been sent,

00:47

header, stuff like that. So this is a

00:49

good example of a context object that's

00:52

used quite a lot in the back end.

00:54

Another example where you might see it

00:55

is if you have some template rendering

00:58

engine like Django or something. It's

01:00

going to have a context that contains

01:02

all the variables and the data that you

01:04

need in order to render that particular

01:06

template. Here's the example we're going

01:08

to work with today. As you can see,

01:10

there is a render article function right

01:12

here that gets a user ID, article ID, a

01:15

database session, a logger, an API key,

01:18

and maybe in the future even more

01:20

things. Then there's a bunch of code

01:22

here. There's some uh check if uh the

01:24

user is allowed to uh do this. We're

01:27

getting an article. If it's not there,

01:29

there's an article not found result. And

01:32

then finally if everything works out as

01:35

we want then we can actually render the

01:37

HTML and return that as a result. Then I

01:40

have a main function that's uh sets up

01:42

some logging. It initialize the database

01:45

creates a session has an API key and it

01:47

then calls render article to render some

01:50

of those articles. Now this doesn't use

01:52

an actual database in the cloud or

01:55

something. This uses um SQL alchemy

01:58

together with a SQLite in memory

02:01

database for just for demo purposes. And

02:04

this is the code for that. So there's an

02:06

article table. There's a function to

02:08

initialize the database. And this also

02:11

fills our in-memory database with a few

02:14

articles. So we can just write some code

02:16

around that. And then here's a function

02:18

that gives us a database session using a

02:21

generator and context manager. So

02:23

nothing too fancy, but at least allows

02:25

us to write some code that kind of

02:27

resembles what you might encounter in

02:29

real life. So let me run this file and

02:31

then this is what we get as a result. So

02:33

it renders the first article and the

02:37

second one basically there's

02:38

unauthorized access by user 41 and

02:42

that's because there is this hard-coded

02:44

check right here. But if I change this

02:46

to user 42, then actually this is an

02:49

article that is not found because the

02:51

there is no article with this ID in our

02:53

inmemory database. So then we get this

02:56

as a result. By the way, if you want to

02:58

learn more about how to design a piece

02:59

of software from scratch, I have a free

03:02

guide for you. You can get it at

03:03

iron.co/designguide.

03:06

This contains the seven steps I take

03:07

whenever I design new software.

03:09

Hopefully it helps you avoid some of the

03:11

mistakes I made in the past.

03:13

Iron.co/designg codes/design guides link

03:15

is in the video description. Now the

03:17

problem we need to solve is that the

03:18

render article has a lot of different

03:21

things that you need to pass as an

03:23

argument. Now you might be able to split

03:26

up this function a bit more. You know

03:28

perhaps uh we could do the authorization

03:30

check somewhere else or maybe we could

03:33

pass the article as an argument uh to

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

واژگان کلیدی (48)

you A1 pronoun

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.

change A1 verb

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.

pass A1 verb

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.

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

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

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

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

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

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