我正在尝试为此类定义 str 方法,以便在打印类的实例时,它会打印一些格式:我的名字是___。我已经上大学__年了,我已经写了__课程。正确的措辞下面是打印,但正确的值不打印。任何帮助将不胜感激。
class Student():
def __init__(self, name, years_at_umich=1):
self.name = name
self.years_UM = years_at_umich
self.bonus_points = random.randrange(1000)
self.programs_written = 0
def __str__(self):
return "My name is %s. I've been in college for %d years and I've written %d programs" \
%(self.name, self.years_UM, self.programs_written)
def shout(self, phrase_to_shout):
print phrase_to_shout
def year_at_umich(self):
return self.years_UM
def write_programs(self, progs=1):
self.programs_written += progs
return self.programs_written
***测试不同的值,目前它只通过了学生的最后一次测试4 ********
class Student_test(unittest.TestCase):
def test_student1(self):
student1 = Student("Lyra")
self.assertEqual(student1.__str__(),"My name is Lyra. I've been at UMich for 1 years and I've written 0 programs.")
def test_student2(self):
student2 = Student("Aisha")
student2.write_programs()
self.assertEqual(student2.__str__(),"My name is Aisha. I've been at UMich for 1 years and I've written 1 programs.")
def test_student3(self):
student3 = Student("Ali",3)
student3.write_programs(4)
self.assertEqual(student3.__str__(),"My name is Ali. I've been at UMich for 3 years and I've written 4 programs.")
def test_student4(self):
student4 = Student("Aja")
student4.write_programs(12)
self.assertEqual(student4.programs_written, 12)
student4.write_programs()
self.assertEqual(student4.programs_written,13)
答案 0 :(得分:1)
class Student():
def shout(num, word):
if num == 1:
phrase = "My name is %s" % word
elif num == 2:
phrase = "I love %s" % word
elif num == 3:
phrase = "My favorite food is %s" % word
return phrase
def years(num):
phrase = "I've been at umich for %s years" % num
return phrase
def programs(num):
phrase = "I've written %s programs" % num
return phrase
Student.shout(1,'Mud')
'我的名字是泥'
答案 1 :(得分:0)
您需要在课程Student
中实施方法__str__或__repr__。如果您只需要按自己的方式打印实例,__str__
就足够了:
def __str__(self):
return "My name is {}. I've written {} programs".format(self.name, self.programs_written)
答案 2 :(得分:0)
您可以通过为类定义__str__
方法来执行此操作,只要将实例转换为字符串,就会调用该方法:
class Student():
# insert the same init here
def shout(self): # we do not need to ask for name because
# we already have it stored in self.name
phrase = "My name is %."
return phrase % self.name # format the text before returning it
# it's also useful to return the string instead
# of immediately printing it in this case because
# we will use it later
def year_at_umich(self):
phrase = "I've been at umich for %s years" # do not modify the instance attribute
return phrase % self.years_UM # use it to format your phrase
def say_number_programs(self): # keeping in pattern with our previous methods
phrase = "I've written %s programs"
return phrase % self.programs_written
def __str__(self):
return '%s. %s and %s.' % (self.shout(), # we can format the final string
self.year_at_munich(), # during the str call
self.say_number_programs)
st = Student('Bob')
print st
返回
My name is Bob. I've been at umich for 1 year and I've written 0 programs.
答案 3 :(得分:0)
如果我理解正确,__str__()
可能会有帮助。
例如:
import random
class Student():
def __init__(self, name, years_at_umich=1):
self.name = name
self.years_UM = years_at_umich
self.bonus_points = random.randrange(1000)
self.programs_written = 0
def __str__(self):
return "My name is %s. I've been in college for %d years and I've written %d programs" \
%(self.name, self.years_UM, self.programs_written)
当您打印学生时,您将看到:
>>>test_stu = Student("Mike")
>>>print test_stu
My name is Mike. I've been in college for 1 years and I've written 0 programs
以下链接可能会有所帮助:
https://docs.python.org/2.7/reference/datamodel.html?highlight=mro#object.str 和 Difference between str and repr in Python