0

我正在做一个文字广告。我有一个房间类和一个项目类,但是当我在房间内放置多个项目时,我会遇到问题。以下是代码示例:

class Place(object):
"""The Room class"""
    def __init__(self, name, Item):
        self.name = name
        self.Item = Item


class Item():
"""The Item class"""
    def __init__(self, name):
        self.name = name


PC = Item("PC")
Bed = Item("Bed")   

bedroom = Place(name = "Bedroom", Item = {PC, Bed})

print (bedroom.Item.name)

和错误信息:

Traceback (most recent call last):
File "C:\Documents and Settings\Kenneth\My Documents\Python_Projects\projects\
TA.Project\location\classes2.py", line 18, in <module>
print (bedroom.Item.name)
AttributeError: 'set' object has no attribute 'name'
Press any key to continue . . .

这在我只有一个项目时有效,但当我尝试添加更多项目时会出错。我试图创建一个 for 循环来一次显示一个项目,但这也不起作用。任何帮助将非常感激。谢谢你。

我尝试的循环类似于:

for i in bedroom.Item:
    print (i)

Attributeerror 'Place' 对象没有属性 'Item'

答案被接受,我想为其他有类似问题的人更新代码:

class Place:
    def __init__(self, name: str, items: set):
        self.name = name
        if items is None:
            items = set()   
        self.items = items

    def get_item_names(self):
    # one-liner: return [x.name for x in self.items]
        names = []
        for item in self.items:
            names.append(item.name)
        return '\n'.join(names)


    def get_item_value(self):
        value = []
        for item in self.items:
            value.append(item.value)
            return value


class Item:
    def __init__(self, name: str, value: iter):
        self.name = name
        self.value = value


Bed=Item("Bed", 200)
Chair=Item("Chair", 10)
Oven=Item("Oven", 500)


bedroom = Place("Bedroom", {Bed, Chair})
kitchen = Place("Kitchen", {Oven})


print (bedroom.get_item_names)

values 函数是为了证明 Item 类仍在工作。

4

1 回答 1

1

不要重用类名作为变量名!这只会混淆并导致错误。:)

对我来说,它可以正常工作bedroom.Item,但也许你交换了一些大写/小写字母?!

AttributeError: 'Place' object has no attribute 'Item'表示您的Place-class 不提供变量Item。因此,如果您实际上将变量命名为itemor Items,您将不得不要求它而不是Item.


在更新问题之前已经写过:

Place当您在一个房间中使用多个物品时,您将提供一组物品,而不是只有一个特定物品。因此,在执行此操作时,bedroom.Item.name您实际上是在要求set存储的 ,bedroom.Items为您提供它name(它没有提供,因此是错误)。

我会建议一个明确的定义。Place总是期待多个项目(也许只有一个,但如果它仍然在一组中,那么我的建议仍然有效)。

例子:

class Place:
    def __init__(self, name: str, items: set):
        # The `items: set` notation is only for better understanding.
        # It is valid Python3 syntax, but does not ensure the actual type!
        self.name = name
        if items is None:
            item = set()   # this is to avoid possible erros later.
        self.items = item

    def get_item_names(self):
        # one-liner: return [x.name for x in self.items]
        names = []
        for item in self.items:
            names.append(item.name)
        return names

class Item:
    def __init__(self, name: str):
        self.name = name

bedroom = Place("Bedroom", {Item("Bed"), Item("Chair")})
kitchen = Place("Kitchen", {Item("Oven")})

# usage:
>>> print(bedroom.get_item_names())
["Bed", "Chair"]
>>> print(kitchen.get_item_names())
["Oven"]

主要区别在于使用循环 (in get_item_names) 来获取所有这些名称。如果您想自己跟踪项目,您仍然items可以获得可以迭代的 -set:

>>> for item in bedroom.items:
...     print(item.name + ":", item)
Bed: <__main__.Item object at 0x7f0c081327f0>
Chair: <__main__.Item obect at 0x7f0c08132898>
于 2015-03-06T09:36:46.790 回答