Python 中的 `object()` 方法有什麼作用?
在 Python 中,使用 `object()` 方法返回一個空物件。它是所有類的基本類。我們來看一下 `object()` 的語法。它不包含任何引數 -
object()
無法向此物件新增新的屬性或方法。它本身是所有屬性和方法的基本類,是任何類預設的。
建立一個空物件
示例
在此示例中,我們將使用 `object()` 方法建立一個空物件 -
# Create an empty object ob = object() # Display the empty object print("Object = ",ob)
輸出
Object = <object object at 0x7f2042320f00>
建立一個空物件並顯示屬性
示例
在此示例中,我們將使用 `object()` 方法建立一個空物件。我們將使用 `dir()` 方法顯示屬性 -
# Create an empty object ob = object() print(dir(ob))
輸出
['__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__']
比較兩個空物件
示例
我們來看看比較兩個空物件會發生什麼。它們將返回 False -
# Create two objects ob1 = object() ob2 = object() # Comparing both then objects print("Are both the objects equal = ",str(ob1 == ob2))
輸出
Are both the objects equal = False
廣告