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'

[Python3 填坑] 013 几个类相关函数的举例的更多相关文章

  1. [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...

  2. [Python3 填坑] 006 “杠零”,空字符的使用

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...

  3. [Python3 填坑] 018 组装类的几个例子

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...

  4. [Python3 填坑] 014 类的常用魔术方法举例

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 __init__() 2.2 __new__() 2.3 __call__() 2.4 __str__() 2.5 __repr__() ...

  5. [Python3 填坑] 009 深拷贝与浅拷贝

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...

  6. [Python3 填坑] 004 关于八进制

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...

  7. [Python3 填坑] 017 实例方法、静态方法、类方法的区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...

  8. [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...

  9. [Python3 填坑] 005 如何“响铃”

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...

随机推荐

  1. 攻防世界--simple-check-100

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/2543a3658d254c30a89e4ea7b8950c27.zip 这道题很坑了, ...

  2. Node 12 值得关注的新特性

    前言 时隔一年,Node.js 12 如约而至,正式发布第一个 Current 版本. 该版本带来了诸如: V8 更新带来好多不错的特性. HTTP 解析速度提升. 启动速度大幅提升. 更好的诊断报告 ...

  3. 2018-2-13-win10-uwp-smms图床

    title author date CreateTime categories win10 uwp smms图床 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 1 ...

  4. 了解Greenplum (2)

    一.目的 1. 理解Greenplum中的数据分布策略(random 和 distribution),分析不同分布策略的优劣:2. 理解查询执行中的数据广播和数据重分布,分析在何种情况下选择哪种策略, ...

  5. AOP拦截日志类,抛异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode

    AOP的日志拦截类中,抛出异常: java.lang.IllegalStateException: It is illegal to call this method if the current r ...

  6. 解决安装mysql-connector-odbc-5.3.2 错误1918……不能加载安装或转换器库……的BUG

    还是在虚拟机Windows Server 2003上安装mysql-connector-odbc-5.3.2,装着装着就报错了,大致是“错误1918……不能加载安装或转换器库……”,问我Retry,I ...

  7. 在 CentOS 上部署 GitLab (自托管的Git项目仓库)

    参考资料https://github.com/mattias-ohlsson/gitlab-installer/blob/master/gitlab-install-el6.sh 环境准备OS: Ce ...

  8. 【leetcode】1091. Shortest Path in Binary Matrix

    题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...

  9. Linux根据进程号查找其程序文件路径 及 lsof 命令使用

    查找进程文件路径 lsof -p pid 1.列出所有打开的文件: lsof 备注: 如果不加任何参数,就会打开所有被打开的文件,建议加上一下参数来具体定位 2. 查看谁正在使用某个文件 lsof   ...

  10. Feign调用远程服务报错:Caused by: java.lang.IllegalStateException: Method getMemberInfo not annotated with HTTP method type (ex. GET, POST)

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ord ...