11 2017 档案
摘要:class doges(object): """类的描述信息""" def __init__(self,name,food): self.name=name self.food=food self.data={}#定义一个类的字典 def __call__(self, *args, **kwargs):#对象后面加括号解执行...
阅读全文
摘要:class MyType(type): def __init__(self,*args,**kwargs): print("Mytype __init__",*args,**kwargs) def __call__(self, *args, **kwargs): print("Mytype __ca
阅读全文
摘要:经典类只@property 没有setter 和deleter 1.23 23.4 1.23 23.4 1.23 23.4 23.4 del attr del attr del attr del attr
阅读全文
摘要:start start start this is a custom exception this is a custom exception this is a custom exception end end end myexception=MyException('231') print(ha
阅读全文
摘要:Django提供一种信号机制。其实就是观察者模式,又叫发布-订阅(Publish/Subscribe) 。当发生一些动作的时候,发出信号,然后监听了这个信号的函数就会执行。 Django内置了一些信号,比如: django.db.models.signals.pre_save 在某个Model保存之
阅读全文
摘要:class SingleObj(): __singleobj=None @classmethod def get_singleinstance(cls): if not cls.__singleobj: cls.__singleobj=SingleObj() return cls.__singleobj obj1=S...
阅读全文
摘要:PS D:\python\py_test> python3 .\t1.py 手动触发exceptionerror: 出错了 print(123) assert 1==1##若成立则执行后面的代码 反之直接报错 print(456)
阅读全文
摘要:obj=Foo()##Foo类是MyType类的对象 所以先执行MyType的init Foo()加括号执行MyType的call foo=type('Foo',(object,),{'say':lambda a:123}) ##所有类都是type类的对象 print(foo().say()) fo
阅读全文
摘要:千万不要被所谓“元类是99%的python程序员不会用到的特性”这类的说辞吓住。因为每个中国人,都是天生的元类使用者 学懂元类,你只需要知道两句话: 道生一,一生二,二生三,三生万物 我是谁?我从哪来里?我要到哪里去? 道生一,一生二,二生三,三生万物 我是谁?我从哪来里?我要到哪里去? 在pyth
阅读全文