07 2020 档案

摘要:__getattr__是当属性不存在的时候运行 __setattr__是设置的时候自动运行 低层实现方法self.__dict__[key]=value __delattr__是删除的时候自动设置 低层实现方法self.__dict__.pop(key) 补充: 上面的是用点方式来使用 __geti 阅读全文
posted @ 2020-07-11 18:56 彡心如止水彡 阅读(129) 评论(0) 推荐(0)
摘要:form m1 import t t.test() #如果导入的t是字符串怎么办 解决办法: 1.0 m=__impotr__("m1.t") print(m) #<module 'm1' (namespace)> 只能获取顶级模块 即m1 m.t.test() 所以要这么调用 2.0 import 阅读全文
posted @ 2020-07-11 18:32 彡心如止水彡 阅读(130) 评论(0) 推荐(0)
摘要:classmethod class Classmethod: def __init__(self,func): self.func=func def __get__(self, instance, owner): def test(*args,**kwargs): return self.func( 阅读全文
posted @ 2020-07-11 18:22 彡心如止水彡 阅读(164) 评论(0) 推荐(0)
摘要:1.使用描述符+类的装饰器 class Typed: def __init__(self,key,type): self.key=key self.type=type def __get__(self, instance, owner): return instance.__dict__[self. 阅读全文
posted @ 2020-07-11 16:36 彡心如止水彡 阅读(648) 评论(0) 推荐(0)
摘要:class Room: def __init__(self,name,length,width): self.name=name self.length=length self.width=width @property def test(self): return self.length*self 阅读全文
posted @ 2020-07-11 15:23 彡心如止水彡 阅读(135) 评论(0) 推荐(0)
摘要:所有类都是元类的对象 class Foo: pass print(type(Foo)) #<class 'type'> 定义类的2种方式: 1. 第一种就是正常的用class来定义 2. Foo=type("Foo",(object,),{"x":1}) type的三个参数: 1.类名 2.继承的类 阅读全文
posted @ 2020-07-11 14:59 彡心如止水彡 阅读(121) 评论(0) 推荐(0)
摘要:isinstance与issubclass的使用 class Foo: pass f=Foo() print(isinstance(f,Foo)) #判断f是否是Foo的实例 class Coo: pass print(issubclass(Coo,Foo)) #判断Coo是否是Foo的子类 c=C 阅读全文
posted @ 2020-07-11 00:12 彡心如止水彡 阅读(101) 评论(0) 推荐(0)
摘要:import redef mul_div(exp): while re.search("[+-]{2,}",exp): exp = exp.replace("--", "+") exp = exp.replace("-+", "-") exp = exp.replace("++", "+") exp 阅读全文
posted @ 2020-07-08 22:20 彡心如止水彡 阅读(189) 评论(0) 推荐(0)