網(wǎng)站首頁 編程語言 正文
hasattr()
hasattr() 函數(shù)用來判斷某個(gè)類實(shí)例對象是否包含指定名稱的屬性或方法。
該函數(shù)的語法格式如下:
hasattr(obj, name)
其中 obj 指的是某個(gè)類的實(shí)例對象,name 表示指定的屬性名或方法名,返回BOOL值,有name特性返回True, 否則返回False。
例子:
class demo:
def __init__ (self):
self.name = "lily"
def say(self):
print("say hi")
d = demo()
print(hasattr(d, 'name'))
print(hasattr(d, 'say'))
print(hasattr(d, 'eat'))
運(yùn)行結(jié)果如下:
True
True
False
getattr()
getattr() 函數(shù)獲取某個(gè)類實(shí)例對象中指定屬性的值。
該函數(shù)的語法格式如下:
getattr(object, name[, default])
其中,obj 表示指定的類實(shí)例對象,name 表示指定的屬性名,而 default 是可選參數(shù),用于設(shè)定該函數(shù)的默認(rèn)返回值,即當(dāng)函數(shù)查找失敗時(shí),如果不指定 default 參數(shù),則程序?qū)⒅苯訄?bào) AttributeError 錯(cuò)誤,反之該函數(shù)將返回 default 指定的值。
例子:
class demo:
def __init__ (self):
self.name = "lily"
def say(self):
return "say hi"
d = demo()
print(getattr(d, 'name'))
print(getattr(d, 'say'))
print(getattr(d, 'eat'))
運(yùn)行結(jié)果如下:
lily
<bound method demo.say of <__main__.demo object at 0x7f31c630d0a0>>
Traceback (most recent call last):
? File "/test.py", line 11, in <module>
? ? print(getattr(d, 'eat'))
AttributeError: 'demo' object has no attribute 'eat'
可以看到,對于類中已有的屬性,getattr() 會返回它們的值,而如果該名稱為方法名,則返回該方法的狀態(tài)信息;反之,如果該明白不為類對象所有,要么返回默認(rèn)的參數(shù),要么程序報(bào) AttributeError 錯(cuò)誤。
需要注意的是,如果是返回的對象的方法,返回的是方法的內(nèi)存地址,如果需要運(yùn)行這個(gè)方法,可以在后面添加一對括號。比如:
class demo:
def __init__ (self):
self.name = "lily"
def say(self):
return "say hi"
def eat(self, something):
return f"eat {something}"
d = demo()
print(getattr(d, 'name'))
print(getattr(d, 'say'))
print(getattr(d, 'eat')('apple'))
print(getattr(d, 'eat', 'no eat')('banana'))
運(yùn)行結(jié)果如下:
lily <bound method demo.say of <__main__.demo object at 0x7fe99b1ca0a0>> eat apple eat banana
setattr()
setattr() 函數(shù)最基礎(chǔ)的功能是修改類實(shí)例對象中的屬性值。其次,它還可以實(shí)現(xiàn)為實(shí)例對象動態(tài)添加屬性或者方法。
該函數(shù)的語法格式如下:
setattr(obj, name, value)
例子:
class demo:
def __init__ (self):
self.name = "lily"
d = demo()
print(getattr(d, 'name'))
print('----------')
setattr(d, 'name', 'tom')
print(getattr(d, 'name'))
print('----------')
print(hasattr(d, 'age'))
setattr(d, 'age', '18')
print(hasattr(d, 'age'))
print(getattr(d, 'age'))
運(yùn)行結(jié)果如下:
lily
----------
tom
----------
False
True
18
原文鏈接:https://blog.csdn.net/zyy247796143/article/details/124334886
相關(guān)推薦
- 2022-05-14 實(shí)現(xiàn)AJAX異步調(diào)用和局部刷新的基本步驟_AJAX相關(guān)
- 2023-02-05 Python實(shí)現(xiàn)前向和反向自動微分的示例代碼_python
- 2022-09-05 淺談異常分類及異常處理機(jī)制
- 2022-08-16 QT布局管理詳解QVBoxLayout與QHBoxLayout及QGridLayout的使用_C 語
- 2022-05-28 Python裝飾器詳細(xì)介紹_python
- 2022-07-07 python中l(wèi)ist*n生成多維數(shù)組與for循環(huán)生成多維數(shù)組的區(qū)別說明_python
- 2022-07-01 python神經(jīng)網(wǎng)絡(luò)Keras實(shí)現(xiàn)GRU及其參數(shù)量_python
- 2022-07-27 numpy中的converters和usecols用法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支