摘要: 重写方法的利器-superclass ilist(list): def __init__(self,dft=None,r=list()): super(ilist, self).__init__(r) #list.__init__(self,r) self.dft=dft def __getitem__(self,n): while len(self) >> 0[0, 0, 0, 0, 0]10由于刚开始不知道super,我是这样模拟x[n]的:class ilist(list): def __init__(self,d... 阅读全文
posted @ 2013-10-31 01:06 LisPythoniC 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 这个方法比较好:class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): self.value = value return value def get(self): return self.valuedh = DataHolder()if dh.set(something()): # do something with dh.valueelif dh.set(somethingelse()): ... 阅读全文
posted @ 2013-10-31 00:19 LisPythoniC 阅读(210) 评论(0) 推荐(0) 编辑
摘要: Classes wishing to support the rich comparison mechanisms must add one or more of the following new special methods: def __lt__(self, other): ... def __le__(self, other): ... def __gt__(self, other): ... ... 阅读全文
posted @ 2013-10-30 20:32 LisPythoniC 阅读(429) 评论(0) 推荐(0) 编辑
摘要: 昨天偶然看见一个算法题,要求返回一个包含给定英文字符串(小写)中所有最长的升序字符串的列表,比如:findmax('abc')-->['abc']findmax('afz')-->['afz']findmax('cba')-->['c','b','a']findmax('zfa')-->['z','f','a']findmax('abczabc')-->[&# 阅读全文
posted @ 2013-10-30 16:36 LisPythoniC 阅读(366) 评论(0) 推荐(0) 编辑
摘要: >>> from collections import defaultdict>>> s='mmississippi'>>> d=defaultdict(int)>>> for i in s: d[i]+=1 >>> print dict(d){'i': 4, 'p': 2, 's': 4, 'm': 2}官方文档解释:class collections.defaultdict([default_factory[, ...]] 阅读全文
posted @ 2013-10-29 23:05 LisPythoniC 阅读(452) 评论(0) 推荐(0) 编辑
摘要: SO上看到一个比较好的问题.大概是:For example I have two dicts:Dict A:{'a':1,'b':2,'c':3}Dict B:{'b':3,'c':4,'d':5}I need a pythonic way of 'combining' two dicts such that the result is :{'a':1,'b':5,'c':7,'d':5}That is to say: 阅读全文
posted @ 2013-10-28 20:42 LisPythoniC 阅读(692) 评论(0) 推荐(0) 编辑
摘要: 该书关于深浅拷贝的论述:6.20. *Copying Python Objects and Shallow and Deep Copies"when shallow copies are made, the string is explicitly copied and a new (string) object created"这是错的.当一个容器对象被浅拷贝,字符串也是和列表一样,都只是建立引用.奇特的是,作者在这句话之前写有自相矛盾的句子:A shallow copy of an object is defined to be a newly created obje 阅读全文
posted @ 2013-10-28 15:48 LisPythoniC 阅读(209) 评论(0) 推荐(0) 编辑
摘要: > (define a '(1 2 3))> (define b (cons a '()))> b((1 2 3))> (set-car! (car b) 100)> b((100 2 3))> a(100 2 3)>从上面可以看到,(car b)和a是同一对象.这个实在是有些危险啊.如何让a和b能够相互独立呢?下面这个函数返回的list各成员和最初传入的list各成员又是相互独立的:;反序 (1 2 3) -> (3 2 1)(define (rvs x) (let recur ((x x)(res '())) (if 阅读全文
posted @ 2013-10-28 14:58 LisPythoniC 阅读(264) 评论(0) 推荐(0) 编辑
摘要: ;反序 (1 2 3) -> (3 2 1)(define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons (car x) res)))));长度 (1 2 3) -> 3(define (len x) (let recur ((x x)(y 0)) (if (null? x) y (recur (cdr x) (+ y 1)))));合并列表 (x ...) (y ...) -> (x ... y ...)(define... 阅读全文
posted @ 2013-10-28 14:16 LisPythoniC 阅读(227) 评论(0) 推荐(0) 编辑
摘要: > (define (f x) x)> (define (g x) (let rec((x x)) x))> (define a '(1 2 3))> (f a)(1 2 3)> (eq? a (f a))#t> (eq? a (g a))#t> (define b (g a))> (set-car! b 10)> a(10 2 3)> 可见,g函数的定义中,named let并未深拷贝x的值,它只是建立传入参数的引用而已.那这也说明,如果一个函数所有参数在递归过程中都会发生改变,那么一般没必要用named let.又如:;切 阅读全文
posted @ 2013-10-28 13:51 LisPythoniC 阅读(377) 评论(0) 推荐(0) 编辑