import math
def add(x,y,f = math.sqrt):
return f(x)+f(y)
print add(25,9)
def add(x,y,f = math.sqrt):
return f(x)+f(y)
print add(25,9)
2016-01-05
def count():
fs = []
for i in range(1, 4):
def f():
print i
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
fs = []
for i in range(1, 4):
def f():
print i
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
2016-01-04
def count():
fs = []
for i in range(1, 4):
def f(i):
return i*i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
return i*i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-01-03
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try :
print Person.__count
except:
print 'attributeerror'
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try :
print Person.__count
except:
print 'attributeerror'
2016-01-03
不得不说python代码要求真是严谨,少个空格都会出错,还好用的sublime有提示功能
def get_grade(self):
if self.__score >= 90:
return "A"
else:
if self.__score >= 60 and self.__score < 90:
return "B"
else:
return "C"
def get_grade(self):
if self.__score >= 90:
return "A"
else:
if self.__score >= 60 and self.__score < 90:
return "B"
else:
return "C"
2015-12-31
import math
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1,101))
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1,101))
2015-12-30
例子太复杂
import time
def performance(f):
t1 = time.time()
def innerf(*args):
return f(*args)
print time.time() - t1
return innerf
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
import time
def performance(f):
t1 = time.time()
def innerf(*args):
return f(*args)
print time.time() - t1
return innerf
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2015-12-29
@property
def grade(self):
return 'A' if self.score >= 80 else ('B' if self.score >= 60 else 'C')
def grade(self):
return 'A' if self.score >= 80 else ('B' if self.score >= 60 else 'C')
2015-12-29
return -1 if self.score >= s.score else (1 if self.score < s.score else 0)
2015-12-29
这OJ好蛋疼……
print "(student:%s,%s,%s)" % (self.name.lower(), self.gender.lower(), self.score)
print "(student:%s,%s,%s)" % (self.name.lower(), self.gender.lower(), self.score)
2015-12-29