for循环仅在__str__语句期间迭代为NoneType?

时间:2011-09-26 00:45:41

标签: python iterator

所以我在Python中尝试创建一个包含ShapeSet个实例列表的Shape实例,我需要它来打印出Shape实例列表。

我可以在代码的其他部分使用for循环而不会遇到错误。但是,当我尝试print语句时,它打印出整个列表,最后导致错误: __str__ returned non-string (type NoneType)

我不明白为什么它不理解停在这里列表的末尾。 (至少那是我认为它正在做的事情。)

非常感谢任何帮助。

class ShapeSet:
    def __init__(self):
        """
        Initialize any needed variables
        """
        self.collect = []
        self.place = None

    def __iter__(self):
        """
        Return an iterator that allows you to iterate over the set of
        shapes, one shape at a time
        """
        self.place = 0
        return self

    def next(self):
        if self.place >= len(self.collect):
            raise StopIteration
        self.place = self.place + 1
        return self.collect[self.place-1]

    def addShape(self, sh):
        """
        Add shape sh to the set; no two shapes in the set may be
        identical
        sh: shape to be added
        """
        s_count = 0
        c_count = 0
        t_count = 0
        self.collect.append(sh)
        for i in self.collect:
            if type(sh) == Square and type(i) == Square:
                if sh.side == i.side:
                    s_count = s_count + 1
                if s_count == 2:
                    self.collect.remove(sh)
                    print('already there')
            if type(sh) == Circle and type(i) == Circle:
                if sh.radius == i.radius:
                    c_count = c_count + 1
                if c_count == 2:
                    self.collect.remove(sh)
                    print('already there')
            if type(sh) == Triangle and type(i) == Triangle:
                if sh.base == i.base and sh.height == i.height:
                    t_count = t_count + 1
                if t_count == 2:
                    self.collect.remove(sh)
                    print('already there')

    def __str__(self):
        """
        Return the string representation for a set, which consists of
        the string representation of each shape, categorized by type
        (circles, then squares, then triangles)
        """
        for i in self.collect:
            if type(i) == Square:
                print ('Square with measurements ' +  str(i.side))
            if type(i) == Circle:
                print ('Circle with measurements ' + str(i.radius))
            if type(i) == Triangle:
                print ('Triangle with measurements, base/height ' + str(i.base)+ ' ' + str(i.height))

2 个答案:

答案 0 :(得分:3)

阅读__str__函数中的docstring。你假设“返回字符串表示”而不是print它。由于return函数中没有__str__语句,因此返回Noneprint扼流圈。

相反,实际上return所需的字符串,并让外部print调用显示它:

def __str__(self):
    """
    Return the string representation for a set, which consists of
    the string representation of each shape, categorized by type
    (circles, then squares, then triangles)
    """
    strings = []
    for i in self.collect:
        if type(i) == Square:
             strings.append('Square with measurements ' +  str(i.side))
        if type(i) == Circle:
            strings.append('Circle with measurements ' + str(i.radius))
        if type(i) == Triangle:
            strings.append('Triangle with measurements, base/height ' + str(i.base)+ ' ' + str(i.height))
    return '\n'.join(strings)

答案 1 :(得分:3)

你写了

def __str__(self):
    """
    **Return** the string representation for a set, which consists of
    the string representation of each shape, categorized by type
    (circles, then squares, then triangles)
    """

但你没有return任何东西 - 你只是打印东西。

在所有课程中使用适当的__str__方法:

class Square:

    def __str__(self):
        return 'Square with measurements ' +  str(i.side)

class Circle:

    def __str__(self):
        return 'Circle with measurements ' + str(i.radius)

# and so on

以及ShapeSet的代表:

class ShapeSet:

    def __str__(self):
        return '\n'.join(str(x) for x in self.collect)

现在您可以print(some_shapeset)以及print(some_circle)