import math
def add(x, y, f):
return f(x) + f(y)
print(add(25, 9, math.sqrt))
def add(x, y, f):
return f(x) + f(y)
print(add(25, 9, math.sqrt))
2016-06-28
# coding=utf-8
class Person(object):
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score >= 90:
print 'A-优秀'
elif self._score >= 60:
print 'B-及格'
else:
print 'C-不
class Person(object):
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score >= 90:
print 'A-优秀'
elif self._score >= 60:
print 'B-及格'
else:
print 'C-不
2016-06-27
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = 'score '
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print AttributeError
def __init__(self, name, score):
self.name = name
self.__score = 'score '
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print AttributeError
2016-06-27
不好意思 應該先算出來後轉int再減去最初的時間的- -
def performance(unit):
def cget(fc):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = fc(*args, **kw)
print 'call %s() in %d ms' % (fc.__name__, int(round(time.time()*1000))-lt)
return fv
return gtime
return cget
def performance(unit):
def cget(fc):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = fc(*args, **kw)
print 'call %s() in %d ms' % (fc.__name__, int(round(time.time()*1000))-lt)
return fv
return gtime
return cget
2016-06-25
def performance(unit):
def cget(func):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = func(*args, **kw)
print 'call %s() in %d ms' % (func.__name__, int(round(time.time()*1000-lt)))
return fv
return gtime
return cget
def cget(func):
def gtime(*args, **kw):
if unit == 'ms':
lt = int(round(time.time()*1000))
fv = func(*args, **kw)
print 'call %s() in %d ms' % (func.__name__, int(round(time.time()*1000-lt)))
return fv
return gtime
return cget
2016-06-25
答案说的有点晦涩 这里的 p1.get_grade()并不是实例方法 跟例子中的p1.get_grade()不一样 因为实例方法需要传入self作为参数 而这里p1.get_grade()只不过是调用lambda:'A'得到返回值A而已 lambda: "A"相当于 def f(): return "A"
2016-06-24
class Person(object):
pass
xiaoming = Person()
xiaohong = Person()
print 'xiao ming '
print 'xiao hong '
print 'xiao ming '== 'xiao hong '
pass
xiaoming = Person()
xiaohong = Person()
print 'xiao ming '
print 'xiao hong '
print 'xiao ming '== 'xiao hong '
2016-06-24
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-06-24