怎么判斷 Python 對(duì)象是否包含某個(gè)屬性
頂級(jí)函數(shù)hasattr可以查看對(duì)象是否包含某某屬性,這里的屬性包括數(shù)據(jù)屬性和方法。getattr可以獲取屬性。
如下舉例說明。
a=[1,2,3]
print('列表有count屬性:%s'%hasattr(a,'count'))
print('列表有append屬性:%s'%hasattr(a,'append'))
print('列表有shift屬性:%s'%hasattr(a,'shift'))
print('列表的count屬性是方法:%s'%hasattr(getattr(a,'count'),'__call__'))
print('列表的append屬性是方法:%s'%hasattr(getattr(a,'append'),'__call__'))
class myclass():
def __init__(self):
self.valattr=3
def method(self):
pass
mc=myclass()
print('myclass有valattr屬性:%s'%hasattr(mc,'valattr'))
print('myclass有method屬性:%s'%hasattr(mc,'method'))
print('myclass的valattr屬性是方法:%s'%hasattr(getattr(mc,'valattr'),'__call__'))
print('myclass的method屬性是方法:%s'%hasattr(getattr(mc,'method'),'__call__'))
仇腫13631096377: 我怎樣才能判斷一個(gè)Python變量是一個(gè)字符串或列表 -
安溪縣節(jié)距: ______ python官方文檔在說明type函數(shù)的用法時(shí),明文推薦用isinstance測(cè)試對(duì)象類型.isinstance似乎不是這么用的. 我通常的做法是用type x=int(5) if type(x)==int: print " x is interger. " else: print "false." isinstance可以用來判斷一個(gè)變量是否屬于一個(gè)類. 在python里應(yīng)該是正確的.if type(x)==list:pass if type(x)==dict:pass
仇腫13631096377: 如何正確理解Python函數(shù)是第一類對(duì)象 -
安溪縣節(jié)距: ______ 函數(shù)作為第一類對(duì)象(First-Class Object)卻是 Python 函數(shù)的一大特性.那究竟什么是第一類對(duì)象呢?在 Python 中萬物皆為對(duì)象,函數(shù)也不例外,函數(shù)作為對(duì)象可以賦值給一個(gè)變量、可以作為元素添加到集合對(duì)象中、可作為參數(shù)值傳遞給其它...
仇腫13631096377: 怎么判斷一個(gè)變量是不是類 python -
安溪縣節(jié)距: ______ python中如何判斷一個(gè)變量的數(shù)據(jù)類型?(原創(chuàng)) 收藏 import types type(x) is types.IntType # 判斷是否int 類型 type(x) is types.StringType #是否string類型 .........-------------------------------------------------------- 超級(jí)惡心的模式,不用記住types.StringType ...
仇腫13631096377: python中如何判斷一個(gè)對(duì)象是某個(gè)類型的數(shù)組 -
安溪縣節(jié)距: ______ 可以使用 Python Image Library 做,load() 函數(shù)會(huì)返回一個(gè)對(duì)象,這個(gè)對(duì)象我們可以把它當(dāng)作一個(gè)二維數(shù)組對(duì)待,而數(shù)組中存放的就是點(diǎn)的 RGB 值,可以很容易地訪問到任何像素點(diǎn)的 RGB 值:from PIL import Image# 可以支持很多種圖片格式.im = Image.open("your_picture.jpg") pix = im.load()# 獲得圖片的尺度,可以用于迭代 print im.size# 獲得某個(gè)像素點(diǎn)的 RGB 值,像素點(diǎn)坐標(biāo)由 [x, y] 指定 print pix[x,y]# 設(shè)置 [x, y] 點(diǎn)的 RGB 的值為 value pix[x,y] = value
仇腫13631096377: python3.2.2 如何判斷輸入的字符串為數(shù)字,int或者float的,是要寫正則表達(dá)式嗎? -
安溪縣節(jié)距: ______ 不用寫正則表達(dá)式也行的,介紹你兩種方法第一種:將變量轉(zhuǎn)換為float型,如果能成功則是數(shù)字,如果拋出錯(cuò)誤則不是數(shù)字.代碼如def isisnu...
如下舉例說明。
a=[1,2,3]
print('列表有count屬性:%s'%hasattr(a,'count'))
print('列表有append屬性:%s'%hasattr(a,'append'))
print('列表有shift屬性:%s'%hasattr(a,'shift'))
print('列表的count屬性是方法:%s'%hasattr(getattr(a,'count'),'__call__'))
print('列表的append屬性是方法:%s'%hasattr(getattr(a,'append'),'__call__'))
class myclass():
def __init__(self):
self.valattr=3
def method(self):
pass
mc=myclass()
print('myclass有valattr屬性:%s'%hasattr(mc,'valattr'))
print('myclass有method屬性:%s'%hasattr(mc,'method'))
print('myclass的valattr屬性是方法:%s'%hasattr(getattr(mc,'valattr'),'__call__'))
print('myclass的method屬性是方法:%s'%hasattr(getattr(mc,'method'),'__call__'))
相關(guān)評(píng)說:
安溪縣節(jié)距: ______ python官方文檔在說明type函數(shù)的用法時(shí),明文推薦用isinstance測(cè)試對(duì)象類型.isinstance似乎不是這么用的. 我通常的做法是用type x=int(5) if type(x)==int: print " x is interger. " else: print "false." isinstance可以用來判斷一個(gè)變量是否屬于一個(gè)類. 在python里應(yīng)該是正確的.if type(x)==list:pass if type(x)==dict:pass
安溪縣節(jié)距: ______ 函數(shù)作為第一類對(duì)象(First-Class Object)卻是 Python 函數(shù)的一大特性.那究竟什么是第一類對(duì)象呢?在 Python 中萬物皆為對(duì)象,函數(shù)也不例外,函數(shù)作為對(duì)象可以賦值給一個(gè)變量、可以作為元素添加到集合對(duì)象中、可作為參數(shù)值傳遞給其它...
安溪縣節(jié)距: ______ python中如何判斷一個(gè)變量的數(shù)據(jù)類型?(原創(chuàng)) 收藏 import types type(x) is types.IntType # 判斷是否int 類型 type(x) is types.StringType #是否string類型 .........-------------------------------------------------------- 超級(jí)惡心的模式,不用記住types.StringType ...
安溪縣節(jié)距: ______ 可以使用 Python Image Library 做,load() 函數(shù)會(huì)返回一個(gè)對(duì)象,這個(gè)對(duì)象我們可以把它當(dāng)作一個(gè)二維數(shù)組對(duì)待,而數(shù)組中存放的就是點(diǎn)的 RGB 值,可以很容易地訪問到任何像素點(diǎn)的 RGB 值:from PIL import Image# 可以支持很多種圖片格式.im = Image.open("your_picture.jpg") pix = im.load()# 獲得圖片的尺度,可以用于迭代 print im.size# 獲得某個(gè)像素點(diǎn)的 RGB 值,像素點(diǎn)坐標(biāo)由 [x, y] 指定 print pix[x,y]# 設(shè)置 [x, y] 點(diǎn)的 RGB 的值為 value pix[x,y] = value
安溪縣節(jié)距: ______ 不用寫正則表達(dá)式也行的,介紹你兩種方法第一種:將變量轉(zhuǎn)換為float型,如果能成功則是數(shù)字,如果拋出錯(cuò)誤則不是數(shù)字.代碼如def isisnu...