isinstance和issubclass
1 # isinstance(obj,cls) 检查obj是否是类cls的对象 2 class Foo: 3 def __init__(self,name): 4 self.name = name 5 class Clss: 6 def __init__(self,name): 7 self.name = name 8 sb = Foo('ssb') 9 print(isinstance(sb, Foo)) # 判断sb是否是Foo的对象 10 sc = Clss('ak') 11 print(isinstance(sc, Foo)) # 判断sc是否是Foo的对象 12 # 返回值 13 # True 14 # False
1 # issubclass(sub, super)检查sub类是否是super的派生类 2 class Foo: 3 def __init__(self): 4 pass 5 class Sb(Foo): 6 def __init__(self): 7 pass 8 print(issubclass(Sb, Foo)) # 判断Sb是否是Foo的子类 9 # 结果 10 # True
反射
反射定义:反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。
python面向对象中的反射:通过字符串的形式操作对象相关属性.python中的一切事物都是对象(都可以使用反射)
# 四个可以实现自省的函数
# 下列方法适用类和对象(一切皆对象,类本身也是一个对象)
def hasattr(*args, **kwargs): # real signature unknown """ Return whether the object has an attribute with the given name. This is done by calling getattr(obj, name) and catching AttributeError. """ pass
hasattr
def getattr(object, name, default=None): # known special case of 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. """ pass
getattr
def setattr(x, y, v): # real signature unknown; restored from __doc__ """ Sets the named attribute on the given object to the specified value. setattr(x, 'y', v) is equivalent to ``x.y = v'' """ pass
setattr
def delattr(x, y): # real signature unknown; restored from __doc__ """ Deletes the named attribute from the given object. delattr(x, 'y') is equivalent to ``del x.y'' """ pass
delattr
1 class Foo: 2 f = 'abc' # 类的静态变量 3 def __init__(self, name, pwd): 4 self.name = name 5 self.pwd = pwd 6 def exex(self): 7 print('hi {}'.format(self.name)) 8 return 'ssd' 9 class Ak: 10 f = 'ssd' 11 def __init__(self): 12 pass 13 sb = Foo('张飞', 38) 14 ak = Ak() 15 16 # hasattr检查是否含有某些属性 17 18 print(hasattr(sb, 'name')) # 检查对象sb中是否含所有name属性 属性名以字符串形式显示 19 print(hasattr(ak, 'name')) # 检查对象ak中是否含所有name属性 属性名以字符串形式显示 20 # 结果 21 # True 22 # False 23 24 # getattr获取属性 有此对象属性就输出没有就报错,可以定制报错形式,使其不报错 25 26 print(getattr(sb, 'name')) # 获取对象的属性 getattr函数 括号第一位填对象 第二位填属性 27 print(getattr(sb, 'exex')) # 获取动态属性 28 print(getattr(sb,'exex')()) # 加括号可以直接调用 29 # print(getattr(sb,'rr')) # 如果没有此属性将报错 30 print(getattr(sb,'rr','不存在')) # 如果没有此属性将报错 可以定制报错形式使其不报错 31 32 # setattr设置属性 原则:如果原本含有此属性便修改其属性,如果不存在此属性,便添加此属性 33 34 setattr(sb, 'sb', 27) # 为对象sb增加属性 属性名为sb 属性值为27 35 print(sb.__dict__) 36 # 结果 {'name': '张飞', 'pwd': 38, 'sb': 27} 37 setattr(sb, 'ak117', 38) # 为对象sb增加属性 属性名为ak117 属性值为38 38 print(sb.__dict__) 39 # 结果:{'name': '张飞', 'pwd': 38, 'sb': 27, 'ak117': 38} 40 setattr(sb, 'ak117', 39) # 对对象sb修改属性 将属性为ak117的属性值修改为39 41 print(sb.__dict__) 42 # 结果 {'name': '张飞', 'pwd': 38, 'sb': 27, 'ak117': 39} 43 setattr(sb, 'show_name', lambda self: self.name+'uu') # 为对象增加动态方法 44 print(sb.show_name(sb)) 45 # 结果:张飞uu 46 47 # delattr删除属性 有就删除没有就报错 48 49 delattr(sb, 'ak117') # 删除 对象sb的ak117属性 50 print(sb.__dict__) 51 # 结果{'name': '张飞', 'pwd': 38, 'sb': 27, 'show_name': <function <lambda> at 0x002DC660>} 52 # delattr(sb, 'rrr') # 如果不存在此属性将会报错 53 # print(sb.__dict__)
四方法演示
1 class Foo: 2 name = '38' 3 def func(): 4 return 'AK117' 5 @staticmethod 6 def funct(): 7 return 'funct' 8 print(getattr(Foo, 'name')) 9 print(getattr(Foo, 'func')()) 10 print(Foo, 'funct')
类也是对象
import sys def s1(): print('s1') def s2(): print('s2') this_module = sys.modules[__name__] print(hasattr(this_module, 's1')) print(getattr(this_module, 's2'))
反射当前模块成员
__str__和__repr__
# 改变对象的字符串显示__str__,__repr__
# 自定制格式化字符串__format__
1 # __str__ 使用方法及含义 2 class Human: 3 def __init__(self, name): 4 self.name = name # 2试验 5 def __str__(self): 6 # return '我是str方法,打印对象时调用的就是我,我是存在于object类中' # 1试验 7 return 'my name is %s' % self.name # 2试验 解释 %s相当于str() 实际上走的是__str__方法 8 # a = Human() # 1试验 9 # print(a) # 1试验 10 # 结果:我是str方法,打印对象时调用的就是我,我是存在于object类中 # 1试验 11 a = Human('明明') # 2试验 12 print(a) # 2试验 13 # 结果 my name is 明明 # 2试验
__str__ 使用方法及含义
1 # __repr__使用方法及含义 2 class Hunman: 3 def __init__(self, name, age): 4 self.name = name 5 self.age = age 6 def __repr__(self): 7 return str(self.__dict__) 8 a = Hunman('Mr.He','Twenty-two') 9 print(repr(a)) # 同理repr函数也是调用内置方法 __repr__ 10 print('>>>%r'% a) # %r 相当于 repr() 或者 __repr__ 11 # 结果 12 # {'name': 'Mr.He', 'age': 'Twenty-two'} 13 # >>>{'name': 'Mr.He', 'age': 'Twenty-two'} 14 # 如果不使用__repr__方法,则返回内存地址
__repr__使用方法及含义
1 # __str__ and __repr__ relationship 2 class Human: 3 def __repr__(self): 4 return str(12345) 5 a = Human() 6 print('%s' % a) 7 # 结果:12345 8 # 原本%s 相当于 __str__方法,但类中无此方法,正常情况下是该报错的。 9 # 但可以正常输出结果-----》结论:__repr__是__str__的备胎。 10 # 如果类中无__str__便去查看父类是否含有,一个父类一个父类找上去直到找到object类中。 11 # 如果没有便找__repr__代替
__str__ and __repr__关系
__len__
# __len__方法 class A: def __init__(self): self.a = 1 self.b = 2 # def __len__(self): # return len(self.__dict__) s = A() print(len(s)) # 直接调用了类中的__len__方法 # 注意 有些方法在object类中是没有被收录的。 # 故如果不构建__len__方法,来读取一个int类型的数据的长度便会报错 # 错误类型为:TypeError: object of type 'A' has no len()
__len__方法
__del__(析构函数)
析构方法,当对象在内存中被释放时,自动触发执行。
注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。
1 # __del__ 2 class Currency: 3 def __del__(self): 4 print('执行了我,删除了你要删除的数据') 5 a = Currency() 6 del a # 即执行了这个方法,又删除了变量
__del__
__call__
1 # __call__ 2 class Human: 3 def __init__(self, name): 4 self.name = name 5 def __call__(self, *args, **kwargs): 6 '''此处可以写一些小方法''' 7 print('abc') 8 a = Human('Mr.he') 9 a() 10 # 结果:abc
__call__
转载于:https://www.cnblogs.com/L5251/articles/8331104.html
原文链接:https://blog.csdn.net/weixin_30342827/article/details/95609370
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~