为什么 VS Code 将单词 “function” 显示为类?

huangapple go评论80阅读模式
英文:

Why does VS Code show the word "function" as a class?

问题

My VS Code将单词function以特殊方式着色,并表示它是一个类:

但它的行为类似于常规名称,例如foo。 它也不是内置类,因为调用它会引发NameError异常。

function这个词有特殊含义吗?

英文:

My VS Code colors the word function distinctively and says it's a class:

为什么 VS Code 将单词 “function” 显示为类?

But it behaves like a regular name such as foo. It's also not a built-in class, as calling it raises a NameError exception.

Does the function word has any special meaning?

答案1

得分: 1

Python提供了一个keyword模块,该模块可以列出语言中存在的硬关键字和软关键字,function不在其中。

import keyword
print(keyword.kwlist)
print(keyword.softkwlist)
print('function' in (keyword.kwlist + keyword.softkwlist)) # False
英文:

Python offers a keyword module that can list both hard and soft keywords present in a language - function is not among them.

import keyword
print(keyword.kwlist)
print(keyword.softkwlist)
print('function' in (keyword.kwlist + keyword.softkwlist)) # False

答案2

得分: 0

function 不是一个保留字,请参考以下内容,根据 Python 文档,详情如下。

https://docs.python.org/3/library/keyword.html?highlight=keywords

https://github.com/python/cpython/blob/3.11/Lib/keyword.py

这个模块允许 Python 程序判断一个字符串是否是关键字或软关键字。

keyword.iskeyword(s)
如果 s 是 Python 关键字,返回 True。

英文:

function is not a reserved word, please see below according to the python documentation, please refer below.

https://docs.python.org/3/library/keyword.html?highlight=keywords

https://github.com/python/cpython/blob/3.11/Lib/keyword.py

This module allows a Python program to determine if a string is a keyword or soft keyword.

keyword.iskeyword(s)
Return True if s is a Python keyword.

答案3

得分: 0

以下是关键词列表:

https://docs.python.org/3/reference/lexical_analysis.html?highlight=keywords#keywords

function 不是其中之一。你可以将 function 作为标识符使用。

例如:

>>> function = 5
>>> function
5

例如 defif 是关键字,你不能这样做。

>>> if = 5
  File "<stdin>", line 1
    if = 5
       ^
SyntaxError: invalid syntax
>>> def = 6
  File "<stdin>", line 1
    def = 6
        ^
SyntaxError: invalid syntax

它也不是内置标识符,就像 printmap 一样:

$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits", or "license" for more information.
>>> function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
英文:

Here is the list of keywords:

https://docs.python.org/3/reference/lexical_analysis.html?highlight=keywords#keywords

function is not one of them. You can e.g. use function as an identifier.

&gt;&gt;&gt; function = 5
&gt;&gt;&gt; function
5

E.g. def or if are keywords and you can not do so.

&gt;&gt;&gt; if = 5
  File &quot;&lt;stdin&gt;&quot;, line 1
    if = 5
       ^
SyntaxError: invalid syntax
&gt;&gt;&gt; def = 6
  File &quot;&lt;stdin&gt;&quot;, line 1
    def = 6
        ^
SyntaxError: invalid syntax

It is also not a built-in identifier as is e.g. print or map:

$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; function
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
NameError: name &#39;function&#39; is not defined

huangapple
  • 本文由 发表于 2023年6月29日 22:00:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定