定义Str方法的问题

时间:2017-03-19 00:42:48

标签: python class methods

我在下面的程序中遇到问题。我正在尝试为此类定义__str__方法,以便在打印类Student的实例时,它会打印一些格式:"My name is ___. I've been in college for __ years and I've written __ programs."任何帮助,将不胜感激。

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(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)

1 个答案:

答案 0 :(得分:0)

注意:这是用Python 3.6编写的

所以我假设您要重新编写 str ()来输出自定义文本。我已经删除了不需要演示此示例的无关代码。这就是我所拥有的:

class Student:
    def __init__(self, name, years_in_school=1):
        self.name = name
        self.years_school = years_in_school
        self.programs_written = 0

    def __str__(self):
        return ("My name is {}, I've been in college for {} years and I've written {} programs!".format(
                self.name, self.years_school, self.programs_written
    ))

首先让我们打破一些事情。在 init ()中,您将years_school分配到years_in_school,为什么不将它们设为相同?这里确实没有必要有两个不同的变量。像这样:

class Student:
    def __init__(self, name, years_in_school=1):
        self.name = name
        self.years_in_school = years_in_school
        self.programs_written = 0

执行 init ()时,可以使用相同的名称。我已经使代码完全匹配你所写的内容,因为我不知道你的意图,所以我发现最好不要为上面的例子改变它。

现在让我们看看能够实现您想要的内容的代码。因此,我们设置 str ()以准确返回您所要求的内容,并使用您已在 init ()中创建的变量对其进行格式化。当你创建你的对象,然后去打印()时,你会看到你正在寻找的东西。

另一种方法是简单地创建一个全新的方法来处理这个问题。您可以简单地创建一个名为:

的新方法,而不是重新定义 str ()
show_student(self):
    print("My name is {}, I've been in college for {} years and I've written {} programs!".format(
           self.name, self.years_school, self.programs_written
))

然后当你打电话时,只需输入:

object_name_goes_here.show_student()

这是更优选的,因为现在当你重新定义 str ()时尝试打印时,它总会输出你创建的字符串和你可能期望的print()。我会诚实地使用定义一种新方法来处理重写 str ()

希望这会帮助你。如果有其他人知道更好的方法,请告诉我。我总是乐于学习更多知识。请参阅附图,了解python控制台中的输出。

Output in Python Console

同样如你的一条评论中所述,years_UM什么也没做,因为你没有要求的变量。您需要将其添加到 init ()并设置为某个值,或者只调用years_school或years_in_school(如果您更改上面提到的代码)。