Python object()函数

描述:

Object类是Python中所有类的基类,如果定义一个类时没有指定继承哪个类,则默认继承object类。

object没有定义__dict__,所以不能对object类实例对象尝试设置属性。

语法:

object()

参数介绍:

返回值:

返回一个新的无特征对象

下面例子展示object()函数使用方法

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 
    b.name = 'wang' # 不能设置属性
AttributeError: 'object' object has no attribute 'name'

本期object()函数就学到这里。

你可能感兴趣的:(Python)