随笔分类 -  Python

优雅高效的语言,学习中……
摘要:写带参数的函数装饰器最纠结的是需要包好多层,最外层是接收参数的函数,它返回一个接收函数的的函数。但这样有个问题是,最终包装出来的装饰器必须加()调用一下,即使没有参数也需要这样做,因为调用这个最外层函数才能返回里面装饰器(就是接收函数的函数)。以前一篇为例,可以这样改进: def opt_arguments(func): def meta_wrapper(*args, **kwargs): i... 阅读全文
posted @ 2014-04-02 20:34 紫红的泪 阅读(1280) 评论(0) 推荐(0) 编辑
摘要:有时候把Python函数调用的命名参数都收集到一个dict中可以更方便地做参数检查,或者直接由参数创建attribute等。更简单的理解就是def foo(*args, **kwargs): pass可以接受所有的参数,其中kwargs就是命名参数字典,那么直接在函数外面套个foo却不能达到目的,一个比较简单的实现是这样的: def annotation(**annotations): """ ... 阅读全文
posted @ 2014-04-02 14:36 紫红的泪 阅读(2694) 评论(0) 推荐(0) 编辑
摘要:使用场景有很多,比如C API在Python下很多都变成了(*args, **kwargs)的参数,这时候可能需要为其添加一个更严格签名来约束参数。 查了许多资料,能有效的拷贝函数签名貌似只能通过动态创建具有新签名的包装函数。使用这一技术的典型范例就是decorator库:https://pypi.python.org/pypi/decorator 下面就根据decorator来实现一个拷贝函... 阅读全文
posted @ 2014-03-28 15:01 紫红的泪 阅读(2516) 评论(0) 推荐(0) 编辑
摘要:想搞一个对象继承自str,然后存一些额外信息用来标识这个字符串,然后理所当然地重写了__init__发现跪了: class newstring(str): def __init__(self, value, othervalue): str.__init__(self, value) self.othervalue = othervalueastring = newstring('hello... 阅读全文
posted @ 2014-03-24 11:56 紫红的泪 阅读(4291) 评论(2) 推荐(0) 编辑
摘要:在某次BugFix中,某哥们儿在/helper/pycharm/tcunittest.py加了个这: 各位亲们可以把True改为False即可解决大量空行的问题。 阅读全文
posted @ 2014-01-28 15:01 紫红的泪 阅读(1346) 评论(0) 推荐(0) 编辑
摘要:我们发现PyQt做的Python版的PyQt是如此好用,如果想把自己的C++库包装成Python模块该如何实现呢? 这里介绍下用SIP包装C++库时值得参考的功能实现: 需要Python模块中实现C++的多态指针类型自动转型为实际类型: 例如:AbstractItem *getItem(); // 这里返回值可能是任何一个子类对象指针,但是在SIP包装后,始终返回AbstractItem对象,... 阅读全文
posted @ 2014-01-27 10:44 紫红的泪 阅读(2092) 评论(0) 推荐(0) 编辑
摘要:# helper class defined elsewhereclass CallLogger(object): def __init__(self, meth): self.meth = meth self.was_called = False def __call__(self, code=None): self.meth() self.was_cal... 阅读全文
posted @ 2013-09-22 18:06 紫红的泪 阅读(2988) 评论(0) 推荐(1) 编辑
摘要:访问顺序: 实例的__getattribute__()、Descriptor的__get__()、实例的__dict__、只读Descriptor的__get__()、实例的__getattr__(); 实例的__setattr__()、Descriptor的__set__()、实例的__dict__; 实例的__delattr__()、Descriptor的__delete_... 阅读全文
posted @ 2013-09-22 13:12 紫红的泪 阅读(314) 评论(0) 推荐(0) 编辑
摘要:Enclose in parentheses: except (IDontLIkeYouException, YouAreBeingMeanException) as e: passSeparating the exception from the variable with a comma will still work in Python 2.6 and 2.7, but is now... 阅读全文
posted @ 2013-09-18 14:45 紫红的泪 阅读(611) 评论(0) 推荐(0) 编辑
摘要:Use __new__ when you need to control the creation of a new instance. Use __init__ when you need to control initialization of a new instance. __new__ is the first step of instance creation. It's ... 阅读全文
posted @ 2013-09-18 11:35 紫红的泪 阅读(557) 评论(0) 推荐(0) 编辑
摘要:发现找不到matplotlib.sphinxext.mathmpl: 可以直接easy_install matplotlib,也可以去这里下载安装包 发现exception: matplotlib requires dateutil错误: 到这里找dateutil安装包(坑爹的easy_install里找不到这个包) 发现exception: matplotlib requ... 阅读全文
posted @ 2013-09-18 11:32 紫红的泪 阅读(2056) 评论(0) 推荐(0) 编辑
摘要:一句话,把Property和Descriptor作用在同一个名字上,就只有Property好使。 阅读全文
posted @ 2013-07-29 17:09 紫红的泪 阅读(309) 评论(0) 推荐(0) 编辑
摘要:在安装traits的时候,发现居然出错了。后来发现安装版的Python 2.7搜索的是Visual Studio 2008,好吧,我电脑里2010和2012都有,真心不想再装个2008。so,先用2012工具链编译吧: 好吧,自己试用的时候这么玩玩就算了,实际产品中一定要用对应版本的编译器。 阅读全文
posted @ 2013-06-13 15:11 紫红的泪 阅读(4083) 评论(1) 推荐(1) 编辑
摘要:从C系语言过来用Python,好不容易适应了写代码不打花括号,突然有一天发现它居然木有枚举……于是stackoverflow了一把,发现神人的枚举(enum)实现到处都是,于是汉化总结过来。 如果是新版Python用户(Python 3.4 with PEP 435): from enum import EnumAnimal = Enum('Animal', 'ant bee ca... 阅读全文
posted @ 2013-05-31 16:44 紫红的泪 阅读(65129) 评论(2) 推荐(10) 编辑
摘要:对于str类型,python并没有reverse函数。然而,通过反向步进切片,我们可以高效地反转一串字符串。 s = 'abcde' s[::-1] 好吧,只支持英文字符。 阅读全文
posted @ 2013-05-31 11:44 紫红的泪 阅读(21080) 评论(0) 推荐(1) 编辑
摘要:JSON doesn't require you to do that, it allows you to do that. It also allows you to use "\u0061" for "A", but it's not required. Allowing \/ helps when embedding JSON in a <script> tag, which doesn't... 阅读全文
posted @ 2013-02-28 17:10 紫红的泪 阅读(11045) 评论(0) 推荐(0) 编辑
摘要:在C#和Python中都有GC,但是它们的实现完全不同。C#用的是传统的垃圾回收机制,主要是寻找能够从根集达到的对象,把这些对象标记为活的,然后清理其余对象;Python由于支持扩展模块(C/C++等),他的根集很难找全,因此Python使用引用计数机制来做垃圾回收。引用计数就存在循环引用的问题,参见How Python GC deal with reference-cycles? ... 阅读全文
posted @ 2013-01-28 14:20 紫红的泪 阅读(443) 评论(0) 推荐(0) 编辑
摘要:Python的super()在多继承下的方法推导顺序(MRO, Method Resolution Order)涉及到C3线性化算法(C3 linearization wiki)。其中关键是, children precede their parents and the order of appearance in __bases__ is respected. 例子, ... 阅读全文
posted @ 2013-01-17 15:54 紫红的泪 阅读(467) 评论(0) 推荐(0) 编辑
摘要:>>> def a():... print "a executed"... return []... >>> >>> def b(x=a()):... x.append(5)... print x... a executed>>> b()[5]>>> b()[5, 5]Actually, this is not a design flaw, a... 阅读全文
posted @ 2013-01-07 16:59 紫红的泪 阅读(1033) 评论(0) 推荐(0) 编辑
摘要:The supplemental garbage collection facility, however, is enabled by default and should be able to free that structure, if none of its components are reachable from the outside anymore and they do not... 阅读全文
posted @ 2013-01-06 16:46 紫红的泪 阅读(897) 评论(0) 推荐(0) 编辑