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

解锁AI学习工具

注册即可使用强大工具,帮助你从每个视频中更快地学习。

场景解析 短语猎手 词卡复习 跟读练习 语音回放
免费注册
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,824 次观看 · 添加于 1 个月前

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.

学习统计

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

你是一个好人。(You are a good person.)

change A1 verb

“改变”意味着使某物变得不同或自身变得不同。

pass A1 verb

经过某物或通过考试。

用练习题学习

从这个视频生成词汇、语法和理解练习

词汇与语法 理解测验 雅思考试 写作练习
注册开始练习
还没有评论。成为第一个分享想法的人!

注册解锁全部功能

追踪进度、保存词汇、练习题目

免费开始学语言