class Person(object):
address='earth'
count=0
def __init__(self,name):
self.name=name
Person.count+=1
address='earth'
count=0
def __init__(self,name):
self.name=name
Person.count+=1
2015-08-05
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-08-05
def count():
fs = []
for i in range(1, 4):
def f(i=i):
return i*i
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i=i):
return i*i
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-08-05
print filter(lambda x: x == x.strip('__'),dir(p))
输出结果:['age', 'course', 'gender', 'name']
输出结果:['age', 'course', 'gender', 'name']
2015-08-04
import operator
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, key = operator.attrgetter('name'))
print L2[0].name
print L2[1].name
print L2[2].name
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, key = operator.attrgetter('name'))
print L2[0].name
print L2[1].name
print L2[2].name
2015-08-04
所以关键是看count调用时刻如何在函数的定义中就复制一份i的值,而不是制造一个同样指向i的新指针。看答案最巧妙的是 继续包一层闭包,不过还是略绕,感觉还是利用函数参数默认值初始化这点实现比较好理解一点。
2015-08-03
import functools
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
这个方便些!
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
这个方便些!
2015-08-03