【二十二】object()函数(1)
【二十二】object()函数(1)
【1】作用
-
Object类是Python中所有类的基类
- 如果定义一个类时没有指定继承哪个类,则默认继承object类。
-
object没有定义__dict__
- 所以不能对object类实例对象尝试设置属性。
【2】语法
object()
- 返回值:
- 返回一个新的无特征对象
【3】示例
class A:
pass
print(issubclass(A,object)) #默认继承object类
print(dir(object)) #object类定义了所有类的一些公共方法
# True
# ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
- object没有定义__dict__
- 所以不能对object类实例对象尝试设置属性
# 定义一个类A
class A:
pass
a = A()
a.name = 'li' # 能设置属性
b = object()
b.name = 'wang' # 不能设置属性
# Traceback (most recent call last):
# File "D:/Pythonproject/111/object.py", line 14, in <module>
# b.name = 'wang' # 不能设置属性
#AttributeError: 'object' object has no attribute 'name'
本文来自博客园,作者:Chimengmeng,转载请注明原文链接:https://www.cnblogs.com/dream-ze/p/17450152.html