[Python3 填坑] 013 几个类相关函数的举例
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 几个类相关函数的举例的更多相关文章
- [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...
- [Python3 填坑] 006 “杠零”,空字符的使用
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...
- [Python3 填坑] 018 组装类的几个例子
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...
- [Python3 填坑] 014 类的常用魔术方法举例
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 __init__() 2.2 __new__() 2.3 __call__() 2.4 __str__() 2.5 __repr__() ...
- [Python3 填坑] 009 深拷贝与浅拷贝
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...
- [Python3 填坑] 004 关于八进制
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...
- [Python3 填坑] 017 实例方法、静态方法、类方法的区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...
- [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...
- [Python3 填坑] 005 如何“响铃”
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...
随机推荐
- sys模块与shutil模块
#coding=utf-8 import sys ## sys.argv #从命令行获取参数 import shutil #文件.文件夹.压缩包.处理模块 f1 = open("test.t ...
- C#设计模式:代理模式(Proxy Pattern)
一,什么是C#设计模式? 代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 二,代码如下: using System; using System.Collectio ...
- Storm简介——初始Storm
一.什么是Storm Strom是由Twitter开源的类似于Hadoop的实时数据处理框架.Strom是分布式流式数据处理系统,强大的分布式集群管理.便捷的针对流式数据的编程模型.高容错保障这些都是 ...
- “没有找到mfc100u.dll”的解决方法
现在需要安装 MindManager 2016 思维导图软件时,打开软件提示找不到 mfc100u.dll,无法执行程序.之前一直好好的,现在换电脑了安装提示这个问题,然后百度找的解决方案: 需要去微 ...
- Apache Mesos 官方文档 V1.0
Apache Mesos 官方文档 V1.0 2016-11-07 中文版:http://mesos.mydoc.io/ gitBook :https://www.gitbook.com/book/m ...
- 卷积神经网络 CNN 系列模型阐述
http://www.sohu.com/a/134347664_642762 Lenet,1986年 https://github.com/BVLC/caffe/blob/master/example ...
- Codeforces Round #421 (Div. 2) - A
题目链接:http://codeforces.com/contest/820/problem/A 题意:一个人在看一本书,书一共C页,这个人每天看v0页,但是他又开始加速看这本书,每天都比前一天多看a ...
- layui树形表格支持非异步和异步加载
layui树形表格支持非异步和异步加载. 仓库地址:https://gitee.com/uniqid/ 使用示例如下: <div class="uui-admin-common-bod ...
- 获取kingeditor编辑器内容
//初始化编辑器 var editorMini = KindEditor.create('.editor-mini',{ width : '70%', height : '250px', resize ...
- 【leetcode】1073. Adding Two Negabinary Numbers
题目如下: Given two numbers arr1 and arr2 in base -2, return the result of adding them together. Each nu ...