可以使用格式说明符,写成这样:
def __str__(self):
return '(student: {0},{1},{2})'.format(self.name, self.gender, self.score)
def __str__(self):
return '(student: {0},{1},{2})'.format(self.name, self.gender, self.score)
2018-03-16
不懂的学员别急,看好题目的提示:任何对象,只要有read()方法,就称为File-like Object,都可以传给json.load()。其实就是让你在类里面定义read方法
2018-03-16
使用了__slots__之后,把super那一行的代码屏蔽了,发现最后的结果也能输出name和gender
还有,把__slots__屏蔽之后,同样不能对属性进行随意增添呀
还有,把__slots__屏蔽之后,同样不能对属性进行随意增添呀
2018-03-15
增加了grade,最后的print后面加了个括号,结果CBA
def grade(self):
if self.__score>=80:
return 'A'
elif self.__score<60:
return 'C'
else:
return 'B'
s = Student('Bob', 59)
print s.grade()
s.score = 60
print s.grade()
s.score = 99
print s.grade()
def grade(self):
if self.__score>=80:
return 'A'
elif self.__score<60:
return 'C'
else:
return 'B'
s = Student('Bob', 59)
print s.grade()
s.score = 60
print s.grade()
s.score = 99
print s.grade()
2018-03-15
def __init__(self, num):
self.num=num
fibo=[0,1]
i=2
while i<self.num:
fibo.append(fibo[i-2]+fibo[i-1])
i = i+1
self.fibo=fibo
def __str__(self):
return str(self.fibo)
def __len__(self):
return len(self.fibo)
self.num=num
fibo=[0,1]
i=2
while i<self.num:
fibo.append(fibo[i-2]+fibo[i-1])
i = i+1
self.fibo=fibo
def __str__(self):
return str(self.fibo)
def __len__(self):
return len(self.fibo)
2018-03-15
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
return 1
elif self.score > s.score:
return -1
elif self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
2018-03-15
# -*- coding: UTF-8 -*-
def get_grade(self):
if self.__score>=90:
return 'A-优秀'
elif self.__score>=60:
return 'B-及格'
else:
return 'C-不及格三档'
def get_grade(self):
if self.__score>=90:
return 'A-优秀'
elif self.__score>=60:
return 'B-及格'
else:
return 'C-不及格三档'
2018-03-15
def calc_prod(lst):
def lazy_prod():
prod = 1
for x in lst:
prod *= x
return prod
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
prod = 1
for x in lst:
prod *= x
return prod
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-15