1. 程式人生 > 其它 >[Python]Python的class(類)中的object是什麼意思

[Python]Python的class(類)中的object是什麼意思

class First_Name():
    print("Cookies")
class Last_Name(object):
    print("Lee")

x=First_Name()
y=Last_Name()
print(dir(x))
print(dir(y))

最後的輸出結果為:

(4Project) PS D:\PythonVirtualEnv\PythonVirtualEnv_391\Scripts> & d:/PythonVirtualEnv/PythonVirtualEnv_391/4Project/Scripts/python.exe d:/PythonVirtualEnv/PythonVirtualEnv_391/tmp.py
Cookies
Lee
['__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__']
['__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__']
(4Project) PS D:\PythonVirtualEnv\PythonVirtualEnv_391\Scripts> python -V
Python 3.9.1
(4Project) PS D:\PythonVirtualEnv\PythonVirtualEnv_391\Scripts> 

如果是在Python2.7.9版本中:

PS D:\PythonVirtualEnv\Python_279> python.exe .\tmp.py
Cookies
Lee
['__doc__', '__module__']
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)]

不繼承object物件,只擁有了__doc__ , __module__ 和自己定義的變數,就是說這個類的名稱空間只有三個物件可以操作;

繼承object物件,擁有了很多可操作物件,這些都是類中的高階特性;

python3 中已經預設載入了object