Python反射和内置方法重写操作详解
作者:—super— 发布时间:2023-07-06 12:00:02
本文实例讲述了Python反射和内置方法重写操作。分享给大家供大家参考,具体如下:
isinstance和issubclass
isinstance(obj,cls)
检查是否obj是否是类 cls 的对象,类似 type()
class Foo(object):
pass
obj = Foo()
isinstance(obj, Foo)
issubclass(sub, super)
检查sub类是否是 super
类的派生类
class Foo(object):
pass
class Bar(Foo):
pass
issubclass(Bar, Foo)
反射
1 什么是反射
反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。
四个反射函数
hasattr(obj,str)
检测是否含有某属性
getattr(obj,str)
获取属性,不存在报错
setattr(obj,str,value)
设置属性
delattr(obj,str)
删除属性,不存在报错
导入其他模块,利用反射查找该模块是否存在某个方法
def test():
print('from the test')
item系列
__getitem__
\__setitem__
\__delitem__
class Foo:
def __init__(self,name):
self.name=name
def __getitem__(self, item):
print(self.__dict__[item])
def __setitem__(self, key, value):
self.__dict__[key]=value
def __delitem__(self, key):
print('del obj[key]时,我执行')
self.__dict__.pop(key)
def __delattr__(self, item):
print('del obj.key时,我执行')
self.__dict__.pop(item)
f1=Foo('sb')
f1['age']=18
f1['age1']=19
del f1.age1
del f1['age']
f1['name']='alex'
print(f1.__dict__)
运行结果:
del obj.key时,我执行
del obj[key]时,我执行
{'name': 'alex'}
__new__
class A:
def __init__(self):
self.x = 1
print('in init function')
def __new__(cls, *args, **kwargs):
print('in new function')
return object.__new__(A, *args, **kwargs)
a = A()
print(a.x)
运行结果:
in new function
in init function
1
单例模式:
class A:
def __new__(cls):
if not hasattr(cls,'obj'):
cls.obj = object.__new__(cls)
return cls.obj
a = A()
b = A()
print(a is b)
运行结果:
True
__call__
对象后面加括号,触发执行。
注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象()
或者 类()()
class Foo:
def __init__(self):
pass
def __call__(self, *args, **kwargs):
print('__call__')
obj = Foo() # 执行 __init__
obj() # 执行 __call__
运行输出:
__call__
__len__
class A:
def __init__(self):
self.a = 1
self.b = 2
def __len__(self):
return len(self.__dict__)
a = A()
print(len(a))
运行结果:
2
__hash__
class A:
def __init__(self):
self.a = 1
self.b = 2
def __hash__(self):
return hash(str(self.a)+str(self.b))
a = A()
print(hash(a))
运行结果:
-1777982230
__eq__
class A:
def __init__(self):
self.a = 1
self.b = 2
def __eq__(self,obj):
if self.a == obj.a and self.b == obj.b:
return True
a = A()
b = A()
print(a == b)
运行结果:
True
合并名字性别一样的人:
class Person:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def __hash__(self):
return hash(self.name+self.sex)
def __eq__(self, other):
if self.name == other.name and self.sex == other.sex:return True
p_lst = []
for i in range(84):
p_lst.append(Person('egon',i,'male'))
print(p_lst)
print(set(p_lst))
运行结果:
[<__main__.Person object at 0x01425AB0>, <__main__.Person object at 0x01425AD0>, <__main__.Person object at 0x01425AF0>, <__main__.Person object at 0x01425910>, <__main__.Person object at 0x014258D0>, <__main__.Person object at 0x01425950>, <__main__.Person object at 0x01425970>, <__main__.Person object at 0x014259D0>, <__main__.Person object at 0x01425C70>, <__main__.Person object at 0x01425890>, <__main__.Person object at 0x01425B30>, <__main__.Person object at 0x01425BB0>, <__main__.Person object at 0x01425C30>, <__main__.Person object at 0x01429710>, <__main__.Person object at 0x01429730>, <__main__.Person object at 0x014298F0>, <__main__.Person object at 0x01429910>, <__main__.Person object at 0x01429930>, <__main__.Person object at 0x01429950>, <__main__.Person object at 0x01429970>, <__main__.Person object at 0x01429990>, <__main__.Person object at 0x014299B0>, <__main__.Person object at 0x014299D0>, <__main__.Person object at 0x014299F0>, <__main__.Person object at 0x01429A10>, <__main__.Person object at 0x01429A30>, <__main__.Person object at 0x01429A50>, <__main__.Person object at 0x01429A70>, <__main__.Person object at 0x01429A90>, <__main__.Person object at 0x01429AB0>, <__main__.Person object at 0x01429AD0>, <__main__.Person object at 0x01429AF0>, <__main__.Person object at 0x01429B10>, <__main__.Person object at 0x01429B30>, <__main__.Person object at 0x01429B50>, <__main__.Person object at 0x01429B70>, <__main__.Person object at 0x01429B90>, <__main__.Person object at 0x01429BB0>, <__main__.Person object at 0x01429BD0>, <__main__.Person object at 0x01429BF0>, <__main__.Person object at 0x01429C10>, <__main__.Person object at 0x01429C30>, <__main__.Person object at 0x01429C50>, <__main__.Person object at 0x01429C70>, <__main__.Person object at 0x01429C90>, <__main__.Person object at 0x01429CB0>, <__main__.Person object at 0x01429CD0>, <__main__.Person object at 0x01429CF0>, <__main__.Person object at 0x01429D10>, <__main__.Person object at 0x01429D30>, <__main__.Person object at 0x01429D50>, <__main__.Person object at 0x01429D70>, <__main__.Person object at 0x01429D90>, <__main__.Person object at 0x01429DB0>, <__main__.Person object at 0x01429DD0>, <__main__.Person object at 0x01429DF0>, <__main__.Person object at 0x01429E10>, <__main__.Person object at 0x01429E30>, <__main__.Person object at 0x01429E50>, <__main__.Person object at 0x01429E70>, <__main__.Person object at 0x01429E90>, <__main__.Person object at 0x01429EB0>, <__main__.Person object at 0x01429ED0>, <__main__.Person object at 0x01429EF0>, <__main__.Person object at 0x01429F10>, <__main__.Person object at 0x01429F30>, <__main__.Person object at 0x01429F50>, <__main__.Person object at 0x01429F70>, <__main__.Person object at 0x01429F90>, <__main__.Person object at 0x01429FB0>, <__main__.Person object at 0x01429FD0>, <__main__.Person object at 0x01429FF0>, <__main__.Person object at 0x01751030>, <__main__.Person object at 0x01751050>, <__main__.Person object at 0x01751070>, <__main__.Person object at 0x01751090>, <__main__.Person object at 0x017510B0>, <__main__.Person object at 0x017510D0>, <__main__.Person object at 0x017510F0>, <__main__.Person object at 0x01751110>, <__main__.Person object at 0x01751130>, <__main__.Person object at 0x01751150>, <__main__.Person object at 0x01751170>, <__main__.Person object at 0x01751190>]
{<__main__.Person object at 0x01425AB0>}
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/pythonclass/p/7373796.html
猜你喜欢
- 1. position:static所有元素的默认定位都是:position:static,这意味着元素没有被定位,而且在文档中出现在它应该
- 如何取回已忘记的密码?forget.asp' 申请<html><head><title>闪亮日子
- 读《论语》,子张十九,子夏曰:博学而笃志,切问而近思,仁在其中矣。 博学:架构需要广度,要尽量多学习各方面的知识。笃志:除了广度,架构师还需
- 大家知道,mailto是网页设计制作中的一个非常实用的html标签,许多拥有个人网页的朋友都喜欢在网站的醒目位置处写上自己的电子邮件地址,这
- MySQL使用环境变量TMPDIR的值作为保存临时文件的目录的路径名。如果未设置TMPDIR,MySQL将使用系统的默认值,通常为/tmp、
- 在Twinsen Liang的博客上看到一篇名为语义化单单的限定在html么?的文章,文中主要是提及了CSS的命名规则,仔细阅读后,我认为这
- 如何用ASP发送HTML格式的邮件?HTML格式的邮件可以把网页上的所有元素,包括文字和图片集成保存在一个文件中,阅读和链接非常便捷,请问在
- 1.having 子句的用法 having 子句对 group by 子句所确定的行组进行控制,having 子句条件中只允许涉及常量,聚组
- 在ASP中,如何获得ADO的连接信息? 具体方法见下列代码:<%Sub Connecti
- 需要准备的工具:SQL Query Analyzer和SqlExec Sunx Version第一部分:去掉xp_cmdshell保护系统的
- 在大型的ASP项目中,很多的页面都涉及到分页、翻页功能。如果每个页面都写一个翻页的程序的话,这样的工作即降低了工作效率,也不利于工程的模块化
- 我们在使用很多新闻系统的时候,都会发现一个问题,尤其是使用 HtmlEdit 从WORD文档中直接拷贝文章(尤其里面有复杂表格和文字)的时候
- 之前写了一个ajax上传工具。但是只是客户端的工具是我写的,服务器端的那个程序,我是修改了一个网上流传的无组件上传类。因为当时我还不懂什么a
- 在照着Tensorflow官网的demo敲了一遍分类器项目的代码后,运行倒是成功了,结果也不错。但是最终还是要训练自己的数据,所以尝试准备加
- 为了降低用户注册难度,国际站的主注册表单一直在改进。主注册三月至今发生了两次较大的变化,现在对表单调整的地方分解如下:1. 两步
- 推荐idea最新激活码:最新Idea激活码永久激活(最新测试有效)https://www.jb51.net/article/178193.h
- CSS布局中可以用javascript判断浏览器版本看如下的javascript脚本: if (window.XMLHt
- <% &nbs
- 技术在进步,思维在发展,网页上的花样当然也要一天天地赶时髦了。在“滚动字符”、“跑马灯”已成平常的今天,网页上还能变出新花样吗?◆制作鼠标指
- 先按照下面的表结构创建mysql_order_by_test数据表,我们用实例一点一点告诉你,MySQL order by的用法。ORDER