class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
print p.__score
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
print p.__score
2016-03-06
def performance(unit):
def perf_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
print 'call',f._name_+'() in',time().unit
return wrapper
return perf_decorator
def perf_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
print 'call',f._name_+'() in',time().unit
return wrapper
return perf_decorator
2016-03-06
这是改正之后的,上一个答案里面 in 前面要加一个空格才能通过。至于为什么time().unit能通过我也不知道,自己乱试的,也许内置有这个时间格式函数 def performance(unit): def performance_decorator(f): def fn(x): print 'call'+f._name_+'() in',time().unit return f(x) return fn return performance_decorator
2016-03-06
def performance(unit):
def performance_decorator(f):
def fn(x):
print 'call'+f._name_+'()in',time().unit
return f(x)
return fn
return performance_decorator
def performance_decorator(f):
def fn(x):
print 'call'+f._name_+'()in',time().unit
return f(x)
return fn
return performance_decorator
2016-03-06
def performance(f):
def fn(x):
print 'call ' +f._name_+'()in',time()
return f(x)
return fn
def fn(x):
print 'call ' +f._name_+'()in',time()
return f(x)
return fn
2016-03-06
解释器内部会将**kw拆分成对应的dict.
setattr()方法接受3个参数:setattr(对象,属性,属性的值)
setattr(self,k,v)相当于self.k = v
kw.iteritems()历遍字典kw的所有key和value,分别匹配k,v
setattr()方法接受3个参数:setattr(对象,属性,属性的值)
setattr(self,k,v)相当于self.k = v
kw.iteritems()历遍字典kw的所有key和value,分别匹配k,v
2016-03-05
def format_name(s):
return s.upper[:1]+s.lower[1:]
print map(, [format_name,['adam', 'LISA', 'barT'])
return s.upper[:1]+s.lower[1:]
print map(, [format_name,['adam', 'LISA', 'barT'])
2016-03-04
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-03-04
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,lambda p1,p2:cmp(p1.name,p2.name))
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,lambda p1,p2:cmp(p1.name,p2.name))
print L2[0].name
print L2[1].name
print L2[2].name
2016-03-04
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
2016-03-04
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})
2016-03-04
import math
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
2016-03-04
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'])
2016-03-04