当前位置: 移动技术网 > IT编程>脚本编程>Python > Python反射和内置方法重写操作详解

Python反射和内置方法重写操作详解

2018年09月23日  | 移动技术网IT编程  | 我要评论

369wyt,红尖山,汝城县人民政府网站

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《python面向对象程序设计入门与进阶教程》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》、《python编码操作技巧总结》及《python入门与进阶经典教程

希望本文所述对大家python程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网