python_面向对象常用函数

issubclass()
作用:检测一个类是否是另一个类的子类
格式:issubclass(子类,父类)
返回值:布尔值
注意事项:只要存在于继承关系中 就成立

isinstance()
作用:检测一个对象是否是指定类的实例
格式:isinstance(对象,类)
返回值:布尔值

hasattr()

作用:检测类/对象是否包含指定名称的成员
格式:hasattr(对象,'成员名称')
返回值:布尔值
注意:可以检测类也可以检测对象,只要可以访问就算存在

getattr()
作用:获取类.对象的成员值
格式:getattr(对象,‘成员名称’,默认值)
返回值:不确定

setattr()
作用:设置类/对象的成员属性值
格式:setattr(对象,‘成员名称’,设置的值)
返回值:无

delattr()
作用:删除类/对象的成员
格式:delattr(对象,‘成员名称’)
返回值:无
dir()
作用:获取对象可以访问的所有成员的列表
格式:dir(对象)
返回值:对象可以访问的所有成员的列表

property() 后面讲
作用:设置描述符操作的函数

# issubclass() 检测一个类是否是另一个类的子类
class Father():
    pass
class Son(Father):
    pass
res = issubclass(Son,Father)
print(res)

# isinstance() 检测指定对象是否是指定类的实例化
res = isinstance([1,2,3,4,5],tuple)
print(res)
class Son():
    pass
wsc = Son()
res = isinstance(wsc,Son)
print(res)

# hasattr()  检测类/对象是否包含指定名称的成员
class ShengWu:
    life = "活着"
class Human(ShengWu):
    name = "hello"
    def move(self):
        print("yidong")
# 实例化一个对象
wenguang = Human()
res = hasattr(wenguang,"life")
print(res)

# getattr()  获取类/对象的成员值
class Human():
    name = "hello"
    def move(self):
        print("yidong")
# 实例化一个对象
def func(self):
    pass
wenguang = Human()
res = getattr(wenguang,"na",func)
print(res)

# setattr()  设置类/对象的成员属性值
# 不存在则新建,存在则修改
class Human():
    __name = "hello"
    def move(self):
        print("yidong")
wenguang = Human()
print(wenguang.__dict__)
setattr(wenguang,"name","alice")
print(wenguang.__dict__)
setattr(wenguang,"name","tommy")
print(wenguang.__dict__)

# 类
print(Human.__dict__)
setattr(Human,"bj","吃了吗?")
print(Human.__dict__)

# delattr()  删除
print(Human.__dict__)
delattr(Human,"name")
print(Human.__dict__)

# dir()
print(dir(wenguang))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值