所有的python对象都没有mro吗?它们不是源自“对象”吗?

时间:2019-09-14 12:57:33

标签: object method-resolution-order

在Python中,一切都源自对象。标量也是对象。 它们不是源自“对象”吗?我们能不能找到基本的 它们来自“对象”?字符串或整数似乎没有mro。

签出以下示例:

$ python
Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class a(object):
...  pass
...
>>> a.__mro__
(<class '__main__.a'>, <class 'object'>)
>>> dir(a)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> a.__dict__
mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'a' objects>, '__weakref__': <attribute '__weakref__' of 'a' objects>, '__doc__': None})
>>> url="Mystring"
>>> type(url)
<class 'str'>
>>> url.__mro__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__mro__'
>>>

0 个答案:

没有答案