为什么我的计数变量不会为我的每个新类实例递增?

时间:2016-12-13 00:28:45

标签: python class

我的代码:

class Lobby(Definition):
    Lcount = 0
    def __init__(self):
        if Lobby.Lcount == 0:
            self.description = "test1"
        elif Lobby.Lcount > 0:
            self.description = "test2"
        else:
            print("\nHmmm something went wrong...\n")
        self.contents = ["Briefcase"]
        self.doors = {"n": "terminal", "w": "hallway"}
        Lobby.Lcount += 1

我希望它是在房间的一个实例创建之后(即你之前已经访问过它),它将显示与原始实例不同的描述。但是,它会保持打印相同的描述。那么我在这里做错了什么呢?

编辑:这是我的定义课程中的内容:

class Definition:

    def __init__(self):
        self.description = ""
        self.contents = []
        self.doors = {}

    def get_desc(self):
        print("{}".format(self.description))

    def get_direction(self):
        direction = input("Enter a direction or search the room: ").lower()
        search = True
        if direction == "q":
            return direction
        elif direction in self.doors:
            location = self.doors[direction]
            return location
        elif direction == "s":
            while search:
                action = input("\nYou search for some items... Press 1 to continue or 2 to quit.\n")
                if action == "1":
                    if len(location.contents) == 0:
                            print("\nYou find nothing of value in the room.\n")
                    else:
                        find = random.randrange(1, 3)
                        if find == 1:
                            found = random.randrange(len(location.contents))
                            item = location.contents.pop(found)
                            print("\nYou found a {}\n".format(item))
                            self.items.append(item)
                            self.check_items()
                        else:
                            print("\nNothing found yet\n")
                elif action == "2":
                    search = False
                    break
                else:
                    print("\nLocation reminder: ")
                    location.get_description()
        else:
            return "\nNot a valid entry\n"

1 个答案:

答案 0 :(得分:0)

Definition中的某些内容阻止了这种情况的正常运作。

class FooClass(object):
    foo = 0
    def __init__(self):
        print("Foo {} before".format(self.__class__.foo))
        self.__class__.foo += 1
        print("Foo {} after".format(self.__class__.foo))
# Works appropriately

>>> x = Foo()
Foo 0 before
Foo 1 after
>>> x = Foo()
Foo 1 before
Foo 2 after

但是,如果它只是一个二进制文件,那么我建议采用不同的方式来跟踪这个问题""变量。限制您创建多个Lobby对象的能力可能会在将来搞砸您。强烈考虑使用visit方法创建一个包含所有对象的字典,该方法运行您__init__现在正在执行的逻辑,并设置一个标记,使得后续访问可以执行不同的操作。

class SomeRoom(Room):
    # where Room is some superclass like Definition is now
    def __init__(self, *args):
        super().__init__(args)  # or however
        self.__visited = False

    @property
    def visited(self):
        return self.__visited

    def visit(self):
        if self.visited:
            print("Welcome back")
        else:
            print("Welcome!")
        self.__visited = True