简体   繁体   English

如何迭代 python 中的默认属性?

[英]how can i iterate default attribute in python?

class Ticket:
    availableSeats = 10

    def __init__(self, availableSeats, eventName, ticketId, date, time, venue):
        self.availableSeats = availableSeats
        self.eventName = eventName
        self.ticketId = ticketId
        self.date = date
        self.time = time

    def updateSeatsAvailable(self):
        self.availableSeats = self.availableSeats-1
        return self.availableSeats

so what i want is when first instance of ticket is create then the attribute availableseats change to 9 and when another instance is created the availableseats change to 8所以我想要的是当创建票的第一个实例时,属性 availableseats 更改为 9,当创建另一个实例时,availableseats 更改为 8

Assuming availableSeats is a property of events , you should have a registry of events mapping event names / event ids to a number of available seats.假设availableSeatsevents的属性,您应该有一个事件注册表,将事件名称/事件 ID 映射到多个可用席位。

I guess this coult be stored on the class, though the intialisation of the tickets makes very little sense:我想这可能会存储在 class 上,尽管票证的初始化意义不大:

class Ticket:
    events = {}

    def __init__(self, availableSeats, eventName, ticketId, date, time, venue):
        self.events.setdefault(eventName, availableSeats)
        self.eventName = eventName
        self.ticketId = ticketId
        self.date = date
        self.time = time

    def updateSeatsAvailable(self):
        self.events[self.eventName] -= 1
        return self.events[self.eventName]

Though as far as I'm concerned, Ticket instances should probably have a link back to an event object, said event object would be storing event-related information (available seats, possibly date, time and venue depending).尽管就我而言, Ticket实例可能应该有一个指向事件 object 的链接,该事件表示事件 object 将存储与事件相关的信息(可用座位,可能取决于日期、时间和地点)。

Conversedly, Ticket should be renamed to Event , the ticketId should not be part of it, possibly other fields shouldn't either depending on the handling of eg multi-day or multi-venue events (but then the event should probably store validation informations relating to these).相反, Ticket应该重命名为Event ,ticketId 不应该是它的一部分,可能其他字段不应该依赖于例如多天或多地点事件的处理(但是事件应该可能存储相关的验证信息对这些)。

Also don't see the point of availableSeats on the class, it's completely unused.也看不到 class 上availableSeats座位的意义,它完全未使用。

And I see no relation whatsoever between the title of the question and the body.而且我认为问题的标题和正文之间没有任何关系。

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

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