def cmp_ignore_case(s1, s2):
return cmp(s1.upper(),s2.upper())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return cmp(s1.upper(),s2.upper())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-12-18
如何去掉`__xxx__`这类的特殊属性,只保留我们自己定义的属性?
print filter(lambda x: x[0:2]!='__', dir(s))
print filter(lambda x: x[0:2]!='__', dir(s))
2015-12-17
class Student(Person):
__slots__ = ('name', 'gender', 'score')
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
__slots__ = ('name', 'gender', 'score')
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
2015-12-15
class Person(object):
__slots__ = ('name', 'gender')
def __init__(self, name, gender):
self.name = name
self.gender = gender
__slots__ = ('name', 'gender')
def __init__(self, name, gender):
self.name = name
self.gender = gender
2015-12-15
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200605560b1bd3c660bf494282ede59fee17e781000
使用__slots__要注意,__slots__定义的属性仅对当前类起作用,对继承的子类是不起作用的:
使用__slots__要注意,__slots__定义的属性仅对当前类起作用,对继承的子类是不起作用的:
2015-12-15
class Fib(object):
lst = []
def __init__(self, num):
a, b = 0, 1
while num:
self.lst.append(a)
a, b = b, a + b
num -= 1
print self.lst
def __len__(self):
return len(self.lst)
f = Fib(5)
print len(f)
lst = []
def __init__(self, num):
a, b = 0, 1
while num:
self.lst.append(a)
a, b = b, a + b
num -= 1
print self.lst
def __len__(self):
return len(self.lst)
f = Fib(5)
print len(f)
2015-12-15
print filter(lambda s : s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2015-12-15
def cmp_ignore_case(s1, s2):
if(s1.lower() > s2.lower()):
return 1
elif s1.lower()< s2.lower():
return -1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if(s1.lower() > s2.lower()):
return 1
elif s1.lower()< s2.lower():
return -1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-12-15
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-12-15
廖老师的课有一定跳跃性,对于很多学生应掌握的细节没有阐述,导致往后学的过程中缺乏过渡的桥梁,比如学完《Python入门》中的招式,再往廖老师的《Python进阶》这门更深入的课程去进攻时,会发现《Python进阶》课程中课时与课时的内容难度太跳跃,有点吃不消。
2015-12-15