最赞回答 / 米乐乐果
参考https://docs.python.org/3/library/functions.html?highlight=property#property
2015-03-26
def __sub__(self, r):
return Rational(self.p * r.q - self.q * r.p, self.q * r.q)
def __mul__(self, r):
return Rational(self.p * r.p , self.q * r.q)
def __div__(self, r):
return Rational(self.p * r.q , self.q * r.p)
return Rational(self.p * r.q - self.q * r.p, self.q * r.q)
def __mul__(self, r):
return Rational(self.p * r.p , self.q * r.q)
def __div__(self, r):
return Rational(self.p * r.q , self.q * r.p)
2015-03-26
def GCD(a,b):
if b==0:
return a
else:
return GCD(b,a%b)
r=GCD(p,q)
self.p = p/r
self.q = q/r
if b==0:
return a
else:
return GCD(b,a%b)
r=GCD(p,q)
self.p = p/r
self.q = q/r
2015-03-26
def calc_prod(lst):
def prod1(x, y):
return x*y
def prod():
#calc the prod
return reduce(prod1, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod1(x, y):
return x*y
def prod():
#calc the prod
return reduce(prod1, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-03-26
import math
def is_sqrt(x):
return math.sqrt(x).is_integer()
def is_sqrt(x):
return math.sqrt(x).is_integer()
2015-03-25
p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
2015-03-25
def get_grade(self):
if self.__score > 80:
return "A-优秀"
elif self.__score >= 60:
return "B-及格"
else:
return "C-不及格"
if self.__score > 80:
return "A-优秀"
elif self.__score >= 60:
return "B-及格"
else:
return "C-不及格"
2015-03-25
#encoding=utf8
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
2015-03-25
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count+1
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count+1
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
2015-03-25