python 判断类是否存在某个属性或方法
生活随笔
收集整理的這篇文章主要介紹了
python 判断类是否存在某个属性或方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python 判斷類是否存在某個屬性或方法
#!/usr/bin/env python # -*- coding:utf-8 -*- #@Time : 2020/5/3 0003 12:47 #@Author : tb_youth #@FileName: hasAtrrTest.py #@SoftWare: PyCharm #@Blog : https://blog.csdn.net/tb_youth''' 判斷類的屬性和方法是否存在'''class A:value = 1def __init__(self):passdef getValue(self):print('value:{}'.format(self.value))def setValue(self,value):self.value = valueif __name__=='__main__':a = A# way1:hasattrop = hasattr(a,'getValue')print(op) #Trueprint(hasattr(a,'AA')) #Falseprint(hasattr(a,'value')) #True# way2:dir# 屬性,方法列表print(dir(a))print('value' in dir(a)) #True True False True ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'getValue', 'setValue', 'value'] True總結(jié)
以上是生活随笔為你收集整理的python 判断类是否存在某个属性或方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python多进程参考代码
- 下一篇: 用不同的姿势求逆序对(复习篇)