python 判断一个类是否有某个属性_Python中如何查看一个类是否包含指定的属性呢?...

摘要:

下文讲述Python中检测一个类是否包含指定属性的方法分享,如下所示:

实现思路:

使用Python中hasattr方法即可检测类是否包含指定属性

hasattr(obj,name)

------参数说明-----

obj:待检测对象

name:属性名称

------返回值说明----

当存在指定属性时,则返回true

否则返回false

例:

Python 检测类是否存在属性的示例分享

#maomao365.com

#类属性存在性判断的示例分享

class UserInfo:

'所有员工的基类'

userQty = 0

def __init__(self, name, salary):

self.name = name

self.age = age

UserInfo.userQty += 1

def displayCount(self):

print (UserInfo.userQty)

def displayEmployee(self):

print ("Name : ", self.name, ", age: ", self.age)

print("hasattr:",hasattr(UserInfo,"userQty"))

print("hasattr:",hasattr(UserInfo,"userQty22"))

python 判断一个类是否有某个属性_Python中如何查看一个类是否包含指定的属性呢?..._第1张图片

Python类中是否包含属性的示例分享

你可能感兴趣的:(python,判断一个类是否有某个属性)