2

我有一个房间类。我希望每次使用该类创建对象时,该对象都会添加到所有房间的列表中。客房等级:

class Rooms:
    """Room class, takes type,days,  occupied or not and when it frees up"""
    def __init__(self, room_type, days, occupied, when_free):
        self.room_type = room_type
        self.days = days
        self.occupied = occupied
        self.when_free = arrow.get(when_free,'YYYY-MM-DD')

任何其他反馈也很感激!

也不确定我是否应该为此创建新主题,但是否有可能在创建对象并将 True onoccuped 传递给对象时,您不需要传递第 4 个变量,而是将其作为当前日期?简而言之,如果没有第四个变量,它会通过 arrow.get(str(arrow.utcnow()),'YYYY-MM-DD')

想出了我的第二个问题。我确实将init更改为:

def __init__(self, room_type, days, occupied, when_free=str(arrow.get(str(arrow.utcnow()),'YYYY-MM-DD'))):
        self.room_type = room_type
        self.days = days
        self.occupied = occupied
        self.when_free = arrow.get(when_free,'YYYY-MM-DD')
4

4 回答 4

5

我会建议一种比上述更优雅和合乎逻辑的方式:

class Building(object):
    def __init__(self):
        self.rooms = []

class Room(object):
    def __init__(self, building=None)

        if building:
            building.rooms.append(self)
        self.building = building

b = Building()
r = Room(b)

这样,您就不需要每次调用b.rooms.append,现在它更符合 OOP。

于 2013-10-10T13:23:51.267 回答
4

理想情况下,您希望房间列表的范围是您计划使用它的地方。不是房间本身的一部分。所以,如果你有一栋带房间的建筑:

class Building():
    def __init__(self):
        self.rooms = []

b = Building()
b.rooms.append(Room(room_type, days, occupied, when_free))

该建筑只是一个例子。重要的部分是rooms.append()。应该在您实际需要使用房间列表的任何地方声明和使用它。

于 2013-10-10T12:39:50.237 回答
1

我在想您可以使用将实例附加到列表的装饰器来装饰__init__方法,以避免在实例注册时弄乱__init__方法。现在,如果您想跟踪实例,您只需向每个类的 init 方法添加一个装饰器。就像是:

#!/usr/bin/env python3

import sys

class InstanceRegister:
    def __call__(self, init):
        def register(instance, *args, **kwargs):
            init(instance, *args, **kwargs)
            try :
                instance.__class__.__instances__
            except:
                instance.__class__.__instances__ = []
            instance.__class__.__instances__.append(instance)
        return register

class Room:
    """Room class, takes type,days,  occupied or not and when it frees up"""
    @InstanceRegister()
    def __init__(self, room_type, days, occupied, when_free):
        self.room_type = room_type
        self.days = days
        self.occupied = occupied
        self.when_free = arrow.get(when_free,'YYYY-MM-DD')

    def __str__(self):
        return "Room of type {:s}".format(self.room_type)



def main():
    r1 = Room('type_a', 1, True, '1999-12-30')
    r2 = Room('type_b', 2, True, '2000-12-30')
    r3 = Room('type_c', 3, True, '2001-01-30')
    for room in Room.__instances__:
        print("{:s}".format(room))
    return 0

if __name__ == '__main__':
    sys.exit(main())

通过12 个简单的步骤了解 Python 装饰器,了解更多关于装饰器的信息!

于 2013-10-10T12:22:52.717 回答
1

将列表设为类变量可能会更好:

class Room(object):
    rooms = []
    def __init__(self, room_type, days, occupied, when_free):
        self.room_type = room_type
        self.days = days
        self.occupied = occupied
        self.when_free = arrow.get(when_free,'YYYY-MM-DD')
        Room.rooms.append(self)

r = Room('x', 1,2, True)
Room.rooms
[<Room object at 0x00000000C6325550>]
r.rooms
[<Room object at 0x00000000C6325550>]

由于它是一个类变量,您可以通过任何类实例或类类型本身来访问它。

编辑为通过“房间”而不是“自我”,这更安全......

于 2013-10-10T12:28:58.497 回答