1. 程式人生 > 程式設計 >Python:type、object、class與內建型別例項

Python:type、object、class與內建型別例項

Python:type、object、class

Python: 一切為物件

>>> a = 1
>>> type(a)
<class'int'>
>>> type(int)
<class'type'>

type => int => 1

type => class => obj

type是個類,生成的類也是物件,生成的例項是物件

>>>class Student:
>>> pass
>>>
>>>stu = Student()
>>>type(stu)
__main__.Student
>>>Student.__base__
object

Student的基類是obj

>>>class MyStudent(Student):
>>> pass
>>>
>>>MyStudent.__base__
__main__.Student

MyStudent繼承Student

MyStudent的基類是Student

object是最頂層的基類

type是個類,同時type也是個物件

>>> type.__base__
object
>>> type(object)
type
>>> object.__bases__
()

object是type的例項

type繼承object

我們可以把這些分為3類:

第一類:type自成一類,type是自己的物件(可以例項化自己),type可以把所有變成他的物件

第二類:list、str、Student…類會繼承object,list、str、Student…是類,同時也是type的物件。object是所有的基類(一切都繼承object)

第三類:生成的物件

Python 內建型別

物件的三個特徵:

身份

型別

身份:每個物件身份均不同

>>> a = 1
>>> id(a)
4333971504
>>> a = {}
>>>id(a)
4393125064

型別

None(全域性只有一個)

數值

int

float

complex(複數)

bool

迭代型別

序列型別

list

bytes、bytearray、memoryview(二進位制序列)

range

tuple

str

array

對映(dict)

集合

set

frozenset

上下文管理型別(with)

其他

模組型別

class和例項

函式型別

方法型別

程式碼型別

object物件

type型別

ellipsis型別

notimplemented類物件

None型別:Python在程式啟動的時候會生成一個None物件

>>> a = None
>>> b = None
>>> id(a) == id(b)
True

以上這篇Python:type、object、class與內建型別例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。