Tutorial Study Image

Python object()

内置函数 object() 返回一个空对象。该对象是所有类的基类。它包含默认的内置属性和方法。不能向此对象添加新的属性或方法。


o = object() #where o is the object
 

object() 参数

object() 函数不接受任何参数。

 

object() 返回值

object() 函数返回一个没有特征的对象。此对象作为所有类的基类。

 

Python 中 object() 方法的示例

示例 1:object() 如何工作?


test = object()

print(type(test))
print(dir(test))
 

输出

test = object()

print(type(test))
print(dir(test))

示例 2:演示 object() 的属性


# declaring the objects of class object
obj1 = object()
obj2 = object()
  
# checking for object equality
print ("Is obj1 equal to obj2 : " + str(obj1 == obj2))
  
# trying to add attribute to object
obj1.name = "GeeksforGeeks"
 

输出

Is obj1 equal to obj2 : False