1. 程式人生 > >Python自省

Python自省

-c true bsp int div type () 運行 source

這個也是python彪悍的特性.

自省就是面向對象的語言所寫的程序在運行時,所能知道對象的類型.簡單一句就是運行時能夠獲得對象的類型.比如type(),dir(),getattr(),hasattr(),isinstance().

a = [1,2,3]
b = {‘a‘:1,‘b‘:2,‘c‘:3}
c = True
print type(a),type(b),type(c) # <type ‘list‘> <type ‘dict‘> <type ‘bool‘>
print isinstance(a,list)  # True

Python自省