gpt4 book ai didi

python - 是否可以在 Python 中重新定义关键字?

转载 作者:太空宇宙 更新时间:2023-11-03 11:15:44 24 4
gpt4 key购买 nike

我遇到了一个问题,我必须对我的 API 设置为返回的所有内容进行 jsonify。当我编写装饰器并将其应用于每个方法时,我想到了一个想法:

"Can't I just overwrite the return keyword so that it performs this operation for me every time?"

我做了一些搜索,但找不到与该主题相关的任何内容。然而,既然“万物皆对象”,或许有可能吧?

显然覆盖 return 是个坏主意,但从更一般的意义上讲,我的问题是:

你能改变 Python 中保留字和关键字的行为吗?

最佳答案

,您不能在 Python 中重新定义保留字。它们的含义是……鼓声……保留,因此根据定义,它不能被更改。

我能找到的最接近显式的 declaration官方文档中的这一事实在语言引用(强调我的)的词法分析章节中:

2.3.1. Keywords

The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:

False      await      else       import     pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

由于关键字不能用作普通标识符,因此不能将其赋值、用作def 语句中的函数名等。

重要的是要了解关键字的基本性质实际上禁止更改它们的含义,尽管如此 - 赋值等不起作用是该性质的结果,而不是其原因。

关于python - 是否可以在 Python 中重新定义关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51992442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com