gpt4 book ai didi

python - 为什么在不继承对象的情况下在 Python 2.7 中定义类不会生成 __mro__ 方法?

转载 作者:太空狗 更新时间:2023-10-29 21:05:40 24 4
gpt4 key购买 nike

<分区>

我正在研究 Mac OS X v10.10 (优胜美地)与 Python 2.7.9。

这是我尝试过的:

  1. 定义一个类

    class A:
    def test(self):
    print "test"

    然后运行

    A.__mro__

    然后我得到了

    >>> A.__mro__
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: class A has no attribute '__mro__'
  2. 然后我定义

    class B(object):
    def test(self):
    print "test"

    然后运行

    B.__mro__

    然后我得到了

    >>> B.__mro__
    (<class '__main__.B'>, <type 'object'>)

这两个定义有什么不同?

我发现在 Python 3 中,没有“object”的版本仍然有 __mro__ 方法。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com