问题描述
当我创建一个包含其唯一内容的模块时:
When I create a module with its sole content:
class Classname(randomobject):
pass
然后我尝试运行解释器说未定义randomobject
的模块的.py文件.
And I try to run the .py file of the module the interpreter says that randomobject
is not defined.
但是当我这样做时:
class Classname(object):
pass
该模块运行正常.那么,如果object
不是关键字,那么它是什么?
The module runs just fine. So if object
is not a keyword, then what is it?
推荐答案
object
是一个(全局)变量.默认情况下,它绑定到内置类,该内置类是类型层次结构的根.
object
is a (global) variable. By default it is bound to a built-in class which is the root of the type hierarchy.
(这带来了有趣的属性,您可以采用任何内置类型,并使用__bases__
属性来达到称为对象的类型).
(This leads to the interesting property that you can take any built-in type, and use the __bases__
property to reach the type called object).
不是关键字或运算符的所有内置内容都是标识符.
Everything built-in that isn't a keyword or operator is an identifier.
这篇关于class Classname(object),Python中的"object"是什么样的词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!