Python'对象'类型和继承 [英] Python 'object' type and inheritance

查看:108
本文介绍了Python'对象'类型和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我可以通过以下方式定义类'foo':

In Python I can define a class 'foo' in the following ways:

class foo:
    pass

class foo(object):
    pass

有什么区别?我试图使用函数issubclass(foo,object)来查看它是否为两个类定义返回True。它没有。

What is the difference? I have tried to use the function issubclass(foo, object) to see if it returns True for both class definitions. It does not.


IDLE 2.6.3      
>>> class foo:
        pass

>>> issubclass(foo, object)
False
>>> class foo(object):
        pass

>>> issubclass(foo, object)
True

谢谢。

推荐答案

继承自对象使一个类成为新式类。这里讨论了旧式和新式: < a href =https://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python> Python中的旧样式和新样式类有什么区别?

Inheriting from object makes a class a "new-style class". There is a discussion of old-style vs. new-style here: What is the difference between old style and new style classes in Python?

正如@CrazyJugglerDrummer在下面评论的那样,在Python 3中,所有类都是新式类。在Python 3中,以下两个声明完全等效:

As @CrazyJugglerDrummer commented below, in Python 3 all classes are "new-style" classes. In Python 3, the following two declarations are exactly equivalent:

class A(object):
    pass

class A:
    pass

这篇关于Python'对象'类型和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆