class Person(object):
count = 0
def __init__(self,name):
Person.count += 1
self.name = name
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self,name):
Person.count += 1
self.name = name
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2019-01-02
使得能打印出<Student: name, gender, score>:
这句有歧义啊
这句有歧义啊
2019-01-01
def count():
fs = []
for i in range(1, 4):
def f(y=i):
return y*y
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(y=i):
return y*y
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-12-31
class Fib(object):
def __call__(self, num):
L = []
for n in range(num):
if n <= 1:
L.append(n)
else:
L.append(L[n-2]+L[n-1])
return L
f = Fib()
print(f(10))
def __call__(self, num):
L = []
for n in range(num):
if n <= 1:
L.append(n)
else:
L.append(L[n-2]+L[n-1])
return L
f = Fib()
print(f(10))
2018-12-30
__slots__ = ('score')
def __init__(self,name,gender,score):
super(Student,self).__init__(name,gender)
self.score = score
def __init__(self,name,gender,score):
super(Student,self).__init__(name,gender)
self.score = score
2018-12-30
@property
def grade(self):
if self.__score>=80:
return 'A'
elif self.__score>=60:
return 'B'
else:
return 'C'
def grade(self):
if self.__score>=80:
return 'A'
elif self.__score>=60:
return 'B'
else:
return 'C'
2018-12-30
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
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)
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
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)
2018-12-30
def __str__(self):
list = []
for i in range(self.num):
if i <= 1:
list.append(i)
else:
list.append(list[i-2]+list[i-1])
for i in range(self.num):
list[i] = str(list[i])
return '[%s]'%(','.join(list))
list = []
for i in range(self.num):
if i <= 1:
list.append(i)
else:
list.append(list[i-2]+list[i-1])
for i in range(self.num):
list[i] = str(list[i])
return '[%s]'%(','.join(list))
2018-12-30
return cmp(self.name,s.name) if self.score==s.score else cmp(s.score,self.score)
2018-12-30
def __str__(self):
return '(%s:%s,%s,%s)'%(self.__class__.__name__,self.name,self.gender,self.score)
return '(%s:%s,%s,%s)'%(self.__class__.__name__,self.name,self.gender,self.score)
2018-12-30
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.items():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k,v in kw.items():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2018-12-30