import functools
# 移动要加 cmp= 这样才会作为默认值参数作为 sorted() 方法的第二个参数,否则会报异常,因为迭代的是函数对象而不是list
sorted_ignore_case = functools.partial(sorted, cmp = lambda x, y : -1 if x.lower() < y.lower() else 1)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
# 移动要加 cmp= 这样才会作为默认值参数作为 sorted() 方法的第二个参数,否则会报异常,因为迭代的是函数对象而不是list
sorted_ignore_case = functools.partial(sorted, cmp = lambda x, y : -1 if x.lower() < y.lower() else 1)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2016-07-30
def count():
fs = []
for i in range(1, 4):
def f(x):
def g():
return x * x
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(x):
def g():
return x * x
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-07-29
title方法是将一个字符串中的每个单词的第一个字母大写,其余的字母小写,如果一个人名是 name = 'john snow'; name.title() 会返回 'John Snow',而 capitalize 是将整个字符串的第一个字母大写,其余的字母小写,name.capitalize() 会返回 'John snow'
2016-07-29
class Person(object):
def __init__(self, name, gender, birth, job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self, name, gender, birth, job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2016-07-29
推荐看一下廖雪峰的官方网站,比这个讲的好多了http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820062641f3bcc60a4b164f8d91df476445697b9e000
2016-07-29
from os.path import isdir,isfile
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
2016-07-29