目录
1. print( 坑的信息 )
- 挖坑时间:2019/04/07
- 明细
坑的编码 | 内容 |
---|---|
Py023-1 | 对 issubclass,isinstance,hasattr,getattr,setattr,delattr 举例 |
2. 开始填坑
2.1 issubclass()
- 官方文档
issubclass(cls, class_or_tuple, /)
Return whether 'cls' is a derived from another class or is the same class.
- 大致意思
- 判断 cls 是否派生自 class_or_tuple,并返回相应的值
- 举例
class A(object):
pass
class B(A):
pass
class C(object):
pass
print("B 是 A 的子类吗:", issubclass(B, A))
print("C 是 A 的子类吗:", issubclass(C, A))
print("B 是 object 的子类吗:", issubclass(B, object))
>>>
B 是 A 的子类吗: True
C 是 A 的子类吗: False
B 是 object 的子类吗: True
2.2 isinstance()
- 官方文档
isinstance(obj, class_or_tuple, /)
Return whether an object is an instance of a class or of a subclass thereof.
- 大致意思
- 判断 obj 是否是 class_or_tuple 的实例,并返回相应的值
- 举例
class A(object):
pass
class B(object):
pass
a = A()
b = B()
print("a 是 A 的实例吗:", isinstance(a, A))
print("A 是 A 的实例吗:", isinstance(A, A))
print("b 是 A 的实例吗:", isinstance(b, A))
>>>
a 是 A 的实例吗: True
A 是 A 的实例吗: False
b 是 A 的实例吗: False
2.3 hasattr()
- 官方文档
hasattr(obj, name, /)
Return whether the object has an attribute with the given name.
- 大致意思
- 判断 obj 是否具有属性 name,并返回相应的值
- 举例
class A():
name = "York"
a = A()
print("a 有属性 name 吗:", hasattr(a, "name" ))
print("a 有属性 age 吗:", hasattr(a, "age" ))
>>>
a 有属性 name 吗: True
a 有属性 age 吗: False
2.4 getattr()
- 官方文档
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object;
getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't exist;
without it, an exception is raised in that case.
- 大致意思
- getattr(x, 'y') 相当于 x.y
- 若 x 有 y 属性,则返回 x 的属性 y
- 若 x 无 y 属性,则引发异常
- 举例
class A():
name = "York"
a = A()
print("a.name: ", getattr(a, "name" ))
print("a.age: ", getattr(a, "age" ))
>>>
a.name: York
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-6ee17f32b693> in <module>()
5
6 print("a.name: ", getattr(a, "name" ))
----> 7 print("a.age: ", getattr(a, "age" ))
AttributeError: 'A' object has no attribute 'age'
2.5 setattr()
- 官方文档
setattr(obj, name, value, /)
Sets the named attribute on the given object to the specified value.
setattr(x, 'y', v) is equivalent to ``x.y = v''
- 大致意思
- 给类属性赋值
- setattr(x, 'y', v) 相当于 x.y = v
- 举例
class A():
name = "York"
a = A()
print("更改前的 a.name: ", a.name)
setattr(a, "name", "Fish")
print("更改前后 a.name: ", a.name)
>>>
更改前的 a.name: York
更改前后 a.name: Fish
2.6 delattr()
- 官方文档
delattr(obj, name, /)
Deletes the named attribute from the given object.
delattr(x, 'y') is equivalent to ``del x.y''
- 大致意思
- 删除类属性
- delattr(x, 'y') 相当于 del x.y
- 举例
class A():
name = "York"
age = 18
a = A()
print("a.name: ", a.name)
print("a.age: ", a.age)
delattr(A, "age")
print("a.name: ", a.name)
print("a.nage ", a.age)
>>>
a.name: York
a.age: 18
a.name: York
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-1b1893332691> in <module>()
11
12 print("a.name: ", a.name)
---> 13 print("a.nage ", a.age)
AttributeError: 'A' object has no attribute 'age'