13 几个类相关函数的举例

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

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

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

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>()
      6 
      7 print("a.name:", getattr(a, "name"))
----> 8 print("a.age:", getattr(a, "age"))

AttributeError: 'A' object has no attribute 'age'

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

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.age", a.age)

>>>

a.name: York
a.age: 18
a.name: York

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-1b1893332691> in <module>()
     12 
     13 print("a.name:", a.name)
---> 14 print("a.age", a.age)

AttributeError: 'A' object has no attribute 'age'
原文地址:https://www.cnblogs.com/yorkyu/p/10693121.html