The concept of (old-style) class is unrelated to the concept of type:
if x is an instance of an old-style class, then x.__class__ designates
the class of x, but type(x) is always <type 'instance'>.
将类更改为从object继承以使其成为新样式的类:
class Class1(object): pass
演示:
>>> class Class1(object): pass
>>> type(Class1())
<class '__main__.Class1'>
The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type 'instance'>.
您必须使用新的样式类,通过从object继承来获得预期的type()结果。你知道吗
>>> class C1: pass
...
>>> class C2(object): pass
...
>>> type(C1())
<type 'instance'>
>>> type(C2())
<class '__main__.C2'>
在Python2中,如果一个类没有从
object
(直接或间接)继承,那么它将被视为一个旧式类。在旧式类中,所有实例都是'instance'
类型。你知道吗从docs:
将类更改为从
object
继承以使其成为新样式的类:演示:
这是python2.x中new-style and classic classes之间的区别之一。事实上:
您必须使用新的样式类,通过从
object
继承来获得预期的type()
结果。你知道吗相关问题 更多 >
编程相关推荐