def gcd(self,p,q):
if p < q:
p ^= q
q ^= p
p ^= q
while q:
tmp = p % q
p = q
q = tmp
return p
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
if p < q:
p ^= q
q ^= p
p ^= q
while q:
tmp = p % q
p = q
q = tmp
return p
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
2015-09-22
def __cmp__(self, s):
if isinstance(s,Student):
return cmp(self.name,s)
else:
return cmp(self.name,str(s))
L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(L)
if isinstance(s,Student):
return cmp(self.name,s)
else:
return cmp(self.name,str(s))
L = [Student('Tim', 99), Student('Bob', 88), 100, 'Hello']
print sorted(L)
2015-09-22
def __cmp__(self, s):
return -1 if self.score > s.score or (self.score == s.score and self.name < s.name) else 0 if self.score == s.score and self.name == s.name else 1
return -1 if self.score > s.score or (self.score == s.score and self.name < s.name) else 0 if self.score == s.score and self.name == s.name else 1
2015-09-22
def __cmp__(self, s):
return -1 if self.score > s.score else 0 if self.score == s.score else 1
return -1 if self.score > s.score else 0 if self.score == s.score else 1
2015-09-22
t1代表的是程序执行前的时间t2代表执行后的时间,但这里的时间是r=f(*args,**kw)运行的时间,这么说来r代表的就是下面的程序。。
2015-09-21
最赞回答 / bear_0008
@log 是Python特有的一种函数简写方式,相当于factorial=log(factorial),将factorial()函数作为log函数的参数进行调用
2015-09-21
已采纳回答 / Max_Geek
f1,f2,f3只是三个变量,你愿意改为 a,b,c 然后打印a() b() c()也可以,三个变量接收count()返回的一个tuple这是我理解
2015-09-21
def count():
fs = []
for i in range(1, 4):
fs.append(lambda i=i: i * i)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
fs.append(lambda i=i: i * i)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-09-21
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))
for i in range(0,len(L2)):
print L2[i].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))
for i in range(0,len(L2)):
print L2[i].name
2015-09-20
#-*- coding:UTF-8-*-
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >= 85:
return 'A-优秀'
else:
return 'B-及格' if self.__score >= 60 else 'C-不及格'
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >= 85:
return 'A-优秀'
else:
return 'B-及格' if self.__score >= 60 else 'C-不及格'
2015-09-19
与Java等不同,类变量在类定义内部的访问也必须添加类名进行访问
class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2015-09-19