def __init__(self,name,gender,birth,**kw):
self.name = name
self.gender = gender
self.birth = birth
for k,v in kw.items():
setattr(self,k,v)
self.name = name
self.gender = gender
self.birth = birth
for k,v in kw.items():
setattr(self,k,v)
2018-02-26
def __init__(self,name,gender,birth,job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
self.name = name
self.gender = gender
self.birth = birth
self.job = job
2018-02-26
class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count+=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
__count = 0
def __init__(self, name):
self.name=name
Person.__count+=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
2018-02-26
import os.path
from os.path import isdir
from os.path import isfile
from os.path import isdir
from os.path import isfile
2018-02-26
sorted_ignore_case = functools.partial(sorted,key = lambda s:s.lower())
2018-02-26
@functools.wraps(f)
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
2018-02-26
def performance_decorator(f):
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
return performance_decorator
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
return performance_decorator
2018-02-26
def is_not_empty(s):
return s and len(s.strip()) > 0
# print filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
print filter(lambda s:s and len(s.strip()) > 0 , ['test', None, '', 'str', ' ', 'END'])
return s and len(s.strip()) > 0
# print filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
print filter(lambda s:s and len(s.strip()) > 0 , ['test', None, '', 'str', ' ', 'END'])
2018-02-26
def count():
fs = []
for i in range(1, 4):
def mul(m = i):
return m*m
fs.append(mul)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def mul(m = i):
return m*m
fs.append(mul)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-02-26
def calc_prod(lst):
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-02-26
def cmp_ignore_case(s1, s2):
if ord(s1[0].lower()) < ord(s2[0].lower()):
return -1
elif ord(s1[0].lower()) > ord(s2[0].lower()):
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if ord(s1[0].lower()) < ord(s2[0].lower()):
return -1
elif ord(s1[0].lower()) > ord(s2[0].lower()):
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-02-26
def is_sqr(x):
a = math.sqrt(x)
return int(a) == float(a)
print filter(is_sqr, range(1, 101))
a = math.sqrt(x)
return int(a) == float(a)
print filter(is_sqr, range(1, 101))
2018-02-26