英文:
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:
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
例如 def
或 if
是关键字,你不能这样做。
>>> if = 5
File "<stdin>", line 1
if = 5
^
SyntaxError: invalid syntax
>>> def = 6
File "<stdin>", line 1
def = 6
^
SyntaxError: invalid syntax
它也不是内置标识符,就像 print
或 map
一样:
$ 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.
>>> function = 5
>>> function
5
E.g. def
or if
are keywords and you can not do so.
>>> if = 5
File "<stdin>", line 1
if = 5
^
SyntaxError: invalid syntax
>>> def = 6
File "<stdin>", 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 "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论