獲取物件資訊

2021-08-15 07:17:45 字數 646 閱讀 9063

print (type(123)==int)

print (type('abc')==str)

import types

def fn():

pass

print(type(fn) == types.functiontype)

print(type(abs) == types.builtinfunctiontype)

print(type(lambda x:x) == types.lambdatype)

print(type((x for x in range(10)))== types.generatortype)

# isinstance()判斷的是乙個物件是否是該型別本身

# 能用type()判斷的基本型別也可以用isinstance()判斷

# 總是優先使用isinstance()判斷型別,可以將指定型別及其子類「一網打盡」

# dir() 函式返回物件的所有屬性和方法,是乙個包含字串的list

print (dir('abc'))

#例項屬性屬於各個例項所有,互不干擾;

#類屬性屬於類所有,所有例項共享乙個屬性;

#不要對例項屬性和類屬性使用相同的名字,否則將產生難以發現的錯誤。

python 獲取物件資訊

判斷物件型別,使用type 函式 判斷乙個物件是否是函式 使用types模組中定義的常量 import types type abs types.builtinfunctiontype true type lambda x x types.lambdatype true type x for x i...

學習筆記 獲取物件資訊

學習日期 2016年9月27日 學習課程 獲取物件資訊 廖雪峰的官方 在本節中,我學習了可以通過type 或者isinstance 可以獲得和判斷物件的型別資訊,他們兩者的不同,在於type 不會認為子類是一種父類型別,isinstance 會認為子類是一種父類型別。還學習了使用dir 可以獲得乙個...

python中獲取物件資訊

拿到乙個變數,除了用isinstance 判斷它是否是某種型別的例項外,還有沒有別的方法獲取到更多的資訊呢?例如,已有定義 class person object def init self,name,gender self.name name self.gender gender class st...