def count():
fs = []
for i in range(1, 4):
def f(j=i):
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j=i):
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-06-23
import time
def performance(f):
def fn(*args,**kw):
start = time.clock()
f(*args,**kw)
end = time.clock()
print end - 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(*args,**kw):
start = time.clock()
f(*args,**kw)
end = time.clock()
print end - start
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2015-06-22
def __sub__(self, r):
return Rationnal(self.p * r.q - self.q * r.p, self.q * r.q)
def __mul__(self, r):
return Rationnal(self.p * r.p, self.q * r.q)
def __div__(self, r):
tmp_r = Rationnal(r.q, r.p)
return self * tmp_r
return Rationnal(self.p * r.q - self.q * r.p, self.q * r.q)
def __mul__(self, r):
return Rationnal(self.p * r.p, self.q * r.q)
def __div__(self, r):
tmp_r = Rationnal(r.q, r.p)
return self * tmp_r
2015-06-22
lambda: 'A'等价于return'A',相当于一个函数f,那么f()='A'.因此,p1.get_grade=f,p1.get_grade()=f()
就是这个意思,但没有说全
p1.get_grade是属性,只不过这里的属性是一个函数对象,即f
p1.get_grade()是方法,前面的p1就是调用这个方法的对象,即实例,整句来说就是实例方法
就是这个意思,但没有说全
p1.get_grade是属性,只不过这里的属性是一个函数对象,即f
p1.get_grade()是方法,前面的p1就是调用这个方法的对象,即实例,整句来说就是实例方法
2015-06-20
def calc_prod(lst):
def f():
def g(x,y):
return x*y //本来这一句不用写的,因为没有内置的求积函数,所以要用reduce函数自己写一个
return reduce(g,lst)
return f
f = calc_prod([1, 2, 3, 4])
print f()
def f():
def g(x,y):
return x*y //本来这一句不用写的,因为没有内置的求积函数,所以要用reduce函数自己写一个
return reduce(g,lst)
return f
f = calc_prod([1, 2, 3, 4])
print f()
2015-06-20
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course
t = Teacher('Alice', 'Female', 'English')
print t.name
print t.course
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course
t = Teacher('Alice', 'Female', 'English')
print t.name
print t.course
2015-06-19
class Person(object):
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'
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'
2015-06-19
class Person(object):
count = 0
def __init__(self,name):
Person.count+=1
self.name=name
count = 0
def __init__(self,name):
Person.count+=1
self.name=name
2015-06-19