随笔分类 - python基础--面向对象进阶
摘要:# # 利用描述符原理定义一个@classmethod # class ClassMethod: # def __init__(self,func): # self.func = func # def __get__(self,instance, owner): # # 类来调用,instance为None,owner为类本身, # # 实例来调用,instance为实例,owner为类本身, #
阅读全文
摘要:class StaticMethod: def __init__(self,func): self.func=func def __get__(self, instance, owner): #类来调用,instance为None,owner为类本身,实例来调用,instance为实例,owner为类本身, print("hhahaha") def feedback(*args,**kwargs)
阅读全文
摘要:# 先回顾一下 class Room: def __init__(self,name,width,length): self.name = name self.width = width self.length = length @property def area(self): return self.width * self.length r1 = Room('wang',100,100) p
阅读全文
摘要:# 描述符,相当于一个代理 class Str: def __init__(self,name): self.name = name # 取值操作时使用 # instance:传入对象 # owner:对象的类 def __get__(self, instance, owner): print("get >",instance,owner) # 返回对象的name属性 return instanc
阅读全文
摘要:class School: def __init__(self,name,addr,type): self.name=name self.addr=addr self.type=type def __repr__(self): return 'School(%s,%s)' %(self.name,self.addr...
阅读全文
摘要:# class Foo: # def __init__(self,x): # self.x = x # def __getattr__(self,item): # print("__getattr__") # # return self.__dict__[item] # def printer(self): # print("lsdajfl") # # f1 = Foo(10) # print(f
阅读全文
摘要:class Foo: x = 1 def __init__(self,y): self.y = y def __getattr__(self,item): print(" >from getattr:你找的属性不存在") def __setattr__(self,key,value): print(' > from setattr') # self.key = value # 这样就无限递归 #
阅读全文
摘要:# 反射主要是程序可以访问,监测和修改它本身状态或行为的一种能力(自省)。 # 四个可以实现自省的函数 # hasattr(object,name) 判断object中有没有一个name字符串对应的方法或者属性 #getattr(obj, attr,default = None): # 调用这个方法将返回obj中名为attr值的属性的值,例如如果attr为'bar',则返回obj.bar。 # 没
阅读全文

浙公网安备 33010602011771号