最赞回答 / 喵呜我来啦
r1 = Rational(1, 3) r2 = Rational(1, 2) r1+r2就是使用__add__方法,即__add__(r1,r2)来实现:带入__add__(self,r)方法中 __add__(r1,r2)的返回值是 Rational( r1.p*r2.q(1*3)+r1.q*r2.p(3*1) , r1.q*r2.q(3*2))=Rational(5,6) 而R...
2018-08-28
def __init__(self, name, gender, **kw):
for k in kw:
setattr(self,k,kw[k])
self.name=name
self.gender=gender
for k in kw:
setattr(self,k,kw[k])
self.name=name
self.gender=gender
2018-08-28
def format_name(s):
head = s[0:1].upper()
tail = s[1:].lower()
return '%s%s' % (head,tail)
print map(format_name, ['adam', 'LISA', 'barT'])
head = s[0:1].upper()
tail = s[1:].lower()
return '%s%s' % (head,tail)
print map(format_name, ['adam', 'LISA', 'barT'])
2018-08-27
闭包中闭包,厉害,r = f(i)这句话是在调用这个函数吧之前在循环中,根本没有掉用过函数,只是将函数作为参数传回,所以,才会错的吧
2018-08-26
class Fib(object):
def __call__(self,num):
lists = [0,1]
for i in range(2,num):
lists.append(lists[i - 1] + lists[i - 2])
return lists
f = Fib()
print f(10)
def __call__(self,num):
lists = [0,1]
for i in range(2,num):
lists.append(lists[i - 1] + lists[i - 2])
return lists
f = Fib()
print f(10)
2018-08-26
已采纳回答 / 慕虎1455308
import math def is_sqr(x): print math.sqrt(x)%1 return math.sqrt(x)%1==0 print filter(is_sqr, range(1, 101))0.00.4142135623730.7320508075690.00.23606797750.4494897427830.6457513110650.8284271247460.00.1622776601680.316624...
2018-08-26