gj2 python中一切皆对象
生活随笔
收集整理的這篇文章主要介紹了
gj2 python中一切皆对象
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2.1 python中一切皆是對象
??? 動態(tài)語言和靜態(tài)語言的區(qū)別,Python的面向?qū)ο蟾鼜氐?/p>
同時動態(tài)語言,代碼的靈活性高
沒有編譯(檢查)的過程,錯誤只有在運行起來后才會發(fā)現(xiàn)
函數(shù)和類也是對象,屬于python的一等公民
????? 1. 賦值給一個變量
????? 2. 可以添加到集合對象中
????? 3. 可以作為參數(shù)傳遞給函數(shù)
????? 4. 可以當(dāng)做函數(shù)的返回值
def ask(name="lewen"):print(name) class Persoon(object):def __init__(self):print("lewen")obj_list = [] obj_list.append(ask) obj_list.append(Persoon) for item in obj_list:print(item())lewen
None
lewen
<__main__.Persoon object at 0x0000029B7B3BBA20>
2.2 type、object和class的關(guān)系
type 的用法 1:生成一個類 2:反回一個對象是什么類型
>>> a = 1 >>> type(a) <class 'int'> >>> type(int) <class 'type'> >>> b = "abc" >>> type(b) <class 'str'> >>> type(str) <class 'type'> >>> class Student: ... pass ... >>> stu = Student() >>> type(stu) <class '__main__.Student'> >>> type(Student) <class 'type'>type->int->1
type->class->obj
type是用來生成類的
>>> type(Student) <class 'type'> >>> Student.__bases__ (<class 'object'>,) >>> class MyStudent(Student): ... pass ... >>> MyStudent.__bases__ (<class '__main__.Student'>,) >>> type.__bases__ (<class 'object'>,) >>> type(object) <class 'type'> >>> object.__bases__ () >>> type(type) <class 'type'># object 是最項層基類
# type 也是一個類,同時type也是一個對象
一切都繼承自object,一切皆對象
type 自己是自己的實例(內(nèi)部通過指針指向同一個內(nèi)存塊)
2.3 python中的常見內(nèi)置類型
對象的三個特征:身份(對象在內(nèi)存中的地址)In [1]: a=1 In [2]: id(a) Out[2]: 140714948027216類型(什么類型的對象)值None(全局只有一個)數(shù)值:int float complex bool迭代類型序列類型listbytes、bytearray、memoryview(二進制序列)rangetuplestrarray映射(dict)集合:set ,frozenset上下文管理類型(with)其他:模塊類型class和實例函數(shù)類型方法類型代碼類型object對象type類型ellipsis類型notimplemented類型總結(jié)
以上是生活随笔為你收集整理的gj2 python中一切皆对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker 容器从入门到Devops实
- 下一篇: gj3 Python数据模型(魔法函数)