简体   繁体   English

Python Tutor中的对象不完整?

[英]Incomplete Object in Python Tutor?

I have this training exercise where I need to find the problems in this program. 我进行了此培训,需要在该程序中查找问题。 I can't find them and python tutor does not allow me to see the object created. 我找不到它们,并且python导师不允许我看到创建的对象。 It just says incomplete object. 它只是说对象不完整。 The program should create a kind of queue for strings. 该程序应为字符串创建一种队列。 What is wrong with the code? 代码有什么问题?

class A():
"""Eine Art Warteschlange für Strings"""

    def __init__(self, wait = [], now = ""):
        self.wait = wait
        self.now = now

    def new_string(self, x):
        """Fügt einen String zur Warteschlange hinzu"""
        self.wait.append(str(x))

    def next_string(self):
        """Holt den nächsten String aus der Warteschlange, speichert ihn als aktuellen String"""
        self.now = self.wait[0]
        self.wait.pop(0)

    def top(self):
        """Gibt den aktuellen String zurück"""
        return self.now

    def __str__(self):
        return self.top

Here are the few bugs I came across in your class.(There may be more): 这是我在课堂上遇到的几个错误(可能还有更多):

  • Under indentation of the class docs. 在类文档的缩进下。
    Fix: Indent it by 4 spaces to the right. 修复:向右缩进4个空格。

  • The __str__ magic method doesn't return a string . __str__ magic方法不会返回string
    Fix: Replace self.top with self.top() . 修复:将self.top替换为self.top() You would want to return the value returned by the method top . 您可能想返回方法top返回的值。

  • Not updating the attribute now whenever new_string is added. 添加new_string now不更新属性。
    Since, you are using the object's now attribute in __str__ , it is advisable to update it whenever an empty object is initialised using new_string method. 由于您正在__str__中使用对象的now属性, __str__ ,每当使用new_string方法初始化空对象时,建议对其进行更新。 There are several ways to do. 有几种方法可以做。 I would suggest you fix this as you see fit. 我建议您按照自己的意愿解决此问题。

Example: 例:

In []: a = A()
In []: a.new_string('foo')
In []: print(a)

In []: a.now
Out[]: ''
In []: a.wait
Out[]: ['foo']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM