7

【Python】【面向对象】BIF

 3 years ago
source link: https://www.guofei.site/2018/08/24/bif.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【Python】【面向对象】BIF

2018年08月24日

Author: Guofei

文章归类: 设计模式 ,文章编号: 1004


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2018/08/24/bif.html

Edit

issubclass

issubclass(class,classinfo) # class是否是classinfo的子类
# 1. 一个类是其自身的子类
# 2. classinfo可以是tuple of class,任意为True就返回True
# 3. 父类和祖先都返回True

isinstance

isinstance(object,classinfo) # 检查是否是一个class的实例
# 1. classinfo是父类或祖先,都返回True
# 2. classinfo可以是tuple of class,任意为True就返回True

hasattr

hasattr(object, name)  # 是否有指定的属性
getattr(object, name[, default])  # 获取属性值,如果不存在且不指定default则报错,如果不存在且指定了default,会返回default
setattr(object, name, value)  # 赋值给一个属性
delattr(object, name)  # 删除一个属性

参考文献:
http://python.jobbole.com/82023/

http://python.jobbole.com/83747/


您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK