class Person(object):
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming==xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming==xiaohong
2015-04-20
try:
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
2015-04-19
import os
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
2015-04-19
import functools
sorted_ignore_case = functools.partial(sorted,cmp=lambda s1,s2:cmp(s1.upper(),s2.upper()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,cmp=lambda s1,s2:cmp(s1.upper(),s2.upper()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2015-04-17
import time
def performance(unit):
def performance_d(f):
def fn(*args,**kw):
print 'call [%s] in %s (%s)' % (f.__name__,time.time(),unit)
return f(*args,**kw)
return fn
return performance_d
def performance(unit):
def performance_d(f):
def fn(*args,**kw):
print 'call [%s] in %s (%s)' % (f.__name__,time.time(),unit)
return f(*args,**kw)
return fn
return performance_d
2015-04-17
import time
def performance(f):
def fn(*args,**kw):
print 'call factorial() in',time.time()
return f(*args,**kw)
return fn
def performance(f):
def fn(*args,**kw):
print 'call factorial() in',time.time()
return f(*args,**kw)
return fn
2015-04-17
print filter(lambda s: s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2015-04-17
def count():
fs = []
for i in range(1, 4):
def f(x=i):
return x*x
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(x=i):
return x*x
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-04-17
def calc_prod(lst):
def chengji():
sum=1
for i in lst:
sum=sum*i
return sum
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
def chengji():
sum=1
for i in lst:
sum=sum*i
return sum
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
2015-04-17
def cmp_ignore_case(s1, s2):
if s1.upper()>s2.upper():
return 1
if s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()>s2.upper():
return 1
if s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-04-17
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2015-04-17