
Python
文章平均质量分 68
upwind_fly
这个作者很懒,什么都没留下…
展开
-
python classmethod用法
需求:添加类对象属性,在新建具体对象时使用该变量class A(): def __init__(self,name): self.name = name self.config = {'batch_size':A.bs} @classmethod def set_bs(cls,bs): cls.bs = bs def print_config(self): print (self.config)A.s...原创 2020-09-01 14:51:15 · 698 阅读 · 0 评论 -
AttributeError: module ‘__main__‘ has no attribute ‘__spec__‘
遇到这个问题是因为在python多进程过程中不能插入断点debug,出现了此报错信息。查找之后在main方法中添加语句解决。https://stackoverflow.com/questions/45720153/python-multiprocessing-error-attributeerror-module-main-has-no-attribute错误信息: AttributeError: module ‘main’ has no attribute ‘spec’完整代码:f.转载 2020-07-17 17:06:38 · 5935 阅读 · 1 评论 -
Python装饰器和import过程
最近阅读tensor2tensor和华为AutoML的源码,发现都用到了装饰器这种用法。简单说就是通过import注册可能用到的类的定义的代码地址,然后后续根据配置需要获得哪个类的实例,在实现过程中都用到了python装饰器,下面就python装饰器的用法做个记录。decor.pydef debug(name=None): print 'Prepare and say...', print name name()@debugdef say(): print "hello原创 2020-07-17 10:25:57 · 917 阅读 · 0 评论 -
compile a string of python code
str = '''flag = Truefor i in range(0,3): print iif flag: print "ok"'''c = compile(str,'','exec')exec c得到如下结果012ok原创 2017-02-10 17:55:52 · 253 阅读 · 0 评论 -
cython加速代码的编写和配置
cython是一种可以加速Python程序运行的方法,打包关键代码且能提升运行速度,除了运行速度的提升,还具有的优势是能调用C++函数库在Python中使用1 安装pip install cython2 编译使用原Python程序可以通过cython编译成so文件提升35%的速度而通过调用C的变量定义,函数定义接口可以提升4倍的速度3 如何编译主要参考https://...原创 2019-09-11 10:46:50 · 505 阅读 · 0 评论