python 內(nèi)置函數(shù)(三): hasattr(), setattr(), getattr(), delattr()

hasattr(object, name)

The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

如果一個(gè)對(duì)象里面有name屬性或者name方法蝇摸,返回True,否則返回False

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'

>>> test = Test('tom')
>>> hasattr(test, 'name')  # object has 'name' attribute
True
>>> hasattr(test, 'hello')  # object has 'hello' function
True

getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

獲取對(duì)象object的屬性或者方法慷妙。如果是返回的對(duì)象的屬性自脯,存在打印出來(lái)嫌套,不存在蛆挫,打印默認(rèn)值琉朽,默認(rèn)值可選淤袜。如果是返回的對(duì)象的方法痒谴,返回的是方法的內(nèi)存地址,可以在后面添加一對(duì)括號(hào)運(yùn)行這個(gè)方法铡羡。

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'
>>> test = Test('tom')
>>> getattr(test, 'name')
'tom'

>>> getattr(test, 'hello')
<bound method Test.hello of <__main__.Test object at 0x7fd09647e110>>

>>> getattr(test, 'hello')()
Hello

>>> getattr(test, 'noexist')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute 'noexist'
'Test' object has no attribute 'noexist'

>>> getattr(test, 'noexist', 'Yes')
'Yes'

setattr(object, name, value)

This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.

給對(duì)象object的屬性賦值积蔚,若屬性不存在,先創(chuàng)建這個(gè)屬性再賦值

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'
>>> test = Test('tom')

>>> hasattr(test, 'age')
False
>>> setattr(test, 'age', 20)
>>> test.age
20
>>> hasattr(test, 'age')
True

delattr(object, name)

This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar.

刪除指定對(duì)象的指定名稱(chēng)的屬性烦周,和setattr函數(shù)作用相反尽爆。

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'
>>> test = Test('tom')
>>> hasattr(test, 'name')
True

>>> test.name
'tom'

>>> delattr(test, 'name')

>>> hasattr(test, 'name')
False

>>> test.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute 'name'
'Test' object has no attribute 'name'

>>> delattr(test, 'name')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: name
name

不能刪除對(duì)象的方法。

>>> test.hello()
Hello

>>> delattr(test, 'hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: hello
hello
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末论矾,一起剝皮案震驚了整個(gè)濱河市教翩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贪壳,老刑警劉巖饱亿,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異闰靴,居然都是意外死亡彪笼,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)蚂且,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)配猫,“玉大人,你說(shuō)我怎么就攤上這事杏死”靡蓿” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵淑翼,是天一觀的道長(zhǎng)腐巢。 經(jīng)常有香客問(wèn)我,道長(zhǎng)玄括,這世上最難降的妖魔是什么冯丙? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮遭京,結(jié)果婚禮上胃惜,老公的妹妹穿的比我還像新娘泞莉。我一直安慰自己,他們只是感情好船殉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布鲫趁。 她就那樣靜靜地躺著,像睡著了一般捺弦。 火紅的嫁衣襯著肌膚如雪饮寞。 梳的紋絲不亂的頭發(fā)上孝扛,一...
    開(kāi)封第一講書(shū)人閱讀 51,562評(píng)論 1 305
  • 那天列吼,我揣著相機(jī)與錄音,去河邊找鬼苦始。 笑死寞钥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的陌选。 我是一名探鬼主播理郑,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼咨油!你這毒婦竟也來(lái)了您炉?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤役电,失蹤者是張志新(化名)和其女友劉穎赚爵,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體法瑟,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冀膝,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了霎挟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窝剖。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖酥夭,靈堂內(nèi)的尸體忽然破棺而出赐纱,到底是詐尸還是另有隱情,我是刑警寧澤熬北,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布疙描,位于F島的核電站,受9級(jí)特大地震影響蒜埋,放射性物質(zhì)發(fā)生泄漏淫痰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一整份、第九天 我趴在偏房一處隱蔽的房頂上張望待错。 院中可真熱鬧籽孙,春花似錦、人聲如沸火俄。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)瓜客。三九已至适瓦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間谱仪,已是汗流浹背玻熙。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留疯攒,地道東北人嗦随。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像敬尺,于是被迫代替她去往敵國(guó)和親枚尼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 函數(shù)調(diào)用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜閱讀 1,170評(píng)論 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評(píng)論 0 10
  • 一個(gè)人生活砂吞,覺(jué)得日子都變長(zhǎng)了――小津安二郎署恍。 做事不用急急忙忙,想到什么要做的事蜻直,都會(huì)慢慢做盯质。生怕等會(huì)沒(méi)...
    木子妍心閱讀 251評(píng)論 0 0
  • 16/07/20 JS與OC的交互 iOS7之前 通過(guò)攔截URL的方式實(shí)現(xiàn)交互 在iOS7之前,實(shí)現(xiàn)js與oc的交...
    朱益達(dá)閱讀 370評(píng)論 0 0
  • 這是一篇不太長(zhǎng)的文章袭蝗,閱讀大約需要3分鐘 不想做將軍的士兵不是好士兵 ——這是在團(tuán)建回來(lái)的路上唤殴,和部門(mén)老大簡(jiǎn)短交流...
    RRRocker閱讀 528評(píng)論 0 0