简体   繁体   中英

What's the difference between 'class A:' and 'class A(object):' in Python?

What's the difference between

class A:
    pass

and

class B(object):
    pass

? For some reason, in methods I can't use super(A, self) but super(B, self) works great. I guess there's no such peculiarity in Py3k :)

In 2.x, the latter creates a new-style class . In 3.x, both have the same effect since old-style classes have been removed.

The first is an old style class. The second is a new-style class. See http://docs.python.org/tutorial/classes.html#multiple-inheritance for a good discussion of the difference. super() only works with new-style classes. http://docs.python.org/library/functions.html#super

B类是一种新样式的类http://www.python.org/doc/newstyle/

As already said the second case creates a new-style class, while the first one creates an old-style class(deprecated!).

New-style classes where created to remove the limitation of the old-styles: old-style classes could not inherit from built-in types. With new-style classes you can inherit from built-in types; in fact all built-in types derive from "object":

>>> list.__mro__
(<type 'list'>, <type 'object'>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM