import time
def performance(f):
def fn(x):
start = time.time()
r = f(x)
print "call " + f.__name__ + "time is :" + str(time.time() - start)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(x):
start = time.time()
r = f(x)
print "call " + f.__name__ + "time is :" + str(time.time() - start)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2015-11-04
一开始用for i in kw:
self.i = kw[i];发现真心不行,因为这个i是一个变量才可以,在js中可以通过self[i]这种中括号实现,但是在python中我试了还是不行,最好么有办法,用setattr实现了,真是囧囧囧!!!!!
self.i = kw[i];发现真心不行,因为这个i是一个变量才可以,在js中可以通过self[i]这种中括号实现,但是在python中我试了还是不行,最好么有办法,用setattr实现了,真是囧囧囧!!!!!
2015-11-04
super(Student,self).__init__()调用父类的方法(这里是构造方法,其它方法是否使用super来调用,还未知)来初始化父类的属性
2015-11-03
(1)、实例属性和类属性重名的时候,实例属性的优先级要高;
(2)、修改实例属性的时候,并不会修改类属性
(2)、修改实例属性的时候,并不会修改类属性
2015-11-03
class Person(object):
count=0;
def __init__(self,name):
Person.count=Person.count+1;
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count;
Person.count = 'milliseconds';
print Person.count;
print p1.count;
count=0;
def __init__(self,name):
Person.count=Person.count+1;
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count;
Person.count = 'milliseconds';
print Person.count;
print p1.count;
2015-11-03
因为sorted第二个参数是一个比较函数,所以就传递了一个cmp函数,cmp=lambda s1,s2:cmp(s1,upper(),s2.upper()),意思就是sorted的第二个参数定义的意思,自己百度
2015-11-02
def __init__(self, name, score):
self.__name=name
self.__score=score
def get_grade(self):
if self.__score>=90:return "A"
elif self.__score>=60:return "B"
else:return "C"
p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
self.__name=name
self.__score=score
def get_grade(self):
if self.__score>=90:return "A"
elif self.__score>=60:return "B"
else:return "C"
p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
2015-11-02
def performance_decorator(f):
def wrapper(*args,**kw):
print "call %s() is %s %s" % (f.__name__,time.time(),unit);
return f(*args,**kw);
return wrapper;
return performance_decorator;
def wrapper(*args,**kw):
print "call %s() is %s %s" % (f.__name__,time.time(),unit);
return f(*args,**kw);
return wrapper;
return performance_decorator;
2015-11-02