Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于排队的人来说,正确的类结构是什么_Python_Class_Queue - Fatal编程技术网

Python 对于排队的人来说,正确的类结构是什么

Python 对于排队的人来说,正确的类结构是什么,python,class,queue,Python,Class,Queue,我想在python中定义一个类,它是一个人员队列 人员具有诸如姓名、年龄、队列中的等级等属性 代码(我有意简短): 我很肯定这不是正确的做法,原因有二: 在类队列中,属性队列与任何内容都不相关 队列外不应存在人员 正如评论中提到的,这取决于您想要实现的功能。 第一个非常基本的例子 import collections class Person: def __init__(self, name, age): self.name = name self.a

我想在python中定义一个类,它是一个人员队列

人员具有诸如姓名、年龄、队列中的等级等属性

代码(我有意简短):

我很肯定这不是正确的做法,原因有二:

  • 在类队列中,属性队列与任何内容都不相关
  • 队列外不应存在人员

  • 正如评论中提到的,这取决于您想要实现的功能。 第一个非常基本的例子

    import collections
    
    
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
        def rank(self, queue):
            return queue.index(self)
    
    
        def __repr__(self):
            return f'Person({self.name}, {self.age})'
    
    
    alice = Person('Alice', 30)
    bob = Person('Bob', 52)
    charlie = Person('Charlie', 40)
    
    queue = collections.deque()
    queue.append(alice) 
    queue.append(bob)
    
    print(queue)
    print(alice.rank(queue)) # this will print 0
    print(charlie.rank(queue)) # this will raise ValueError
    
    输出

    deque([Person(Alice, 30), Person(Bob, 52)])
    0
    Traceback (most recent call last):
      File "***", line 28, in <module>
        print(charlie.rank(queue)) # this will raise ValueError
      File "***", line 11, in rank
        return queue.index(self)
    ValueError: Person(Charlie, 40) is not in deque
    
    输出:

    Queue([Person(Alice, 30), Person(Bob, 52)])
    0
    Traceback (most recent call last):
      File "***", line 53, in <module>
        print(charlie.rank) # this will raise ValueError
      File "***", line 16, in rank
        raise ValueError(f'{self} not in queue')
    ValueError: Person(Charlie, 40) not in queue
    
    Queue([Person(爱丽丝,30岁),Person(鲍勃,52岁)])
    0
    回溯(最近一次呼叫最后一次):
    文件“***”,第53行,在
    print(charlie.rank)#这将引起值错误
    文件“***”,第16行,排名
    raise VALUERROR(f'{self}不在队列中')
    ValueError:人员(Charlie,40岁)不在队列中
    

    综上所述,您的问题可能更适合,也可能被认为是基于[可能]观点的离题问题。

    您的代码没有任何作用。对于队列-查看
    集合。deque
    <代码>人员。不应分配队列中的排名。将其设置为属性(例如,使用
    @property
    )并在需要时返回队列中的当前位置。很难确切地知道您需要什么或您要做什么,但从技术上讲,您拥有的并没有错。虽然如果这就是队列所做的一切,那么我不知道你为什么不使用列表。确保一个人不应该出现在队列之外似乎不是队列的责任。如果您想让队列自己创建Person对象,那么您需要让队列自己创建Person对象。@saquintes您能准确说明“您需要让队列自己创建Person对象”的意思吗?@buran您能在回答中进一步说明您的意见吗?
    import collections
    
    
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
            self._queue = None # single leading underscore indicates that it is for internal use only.
    
    
        @property
        def rank(self):
            if self._queue:
                return self._queue.index(self)
            else:
                raise ValueError(f'{self} not in queue')
    
    
        @property
        def queue(self):
            return self._queue
    
    
        @queue.setter
        def queue(self, value):
            if value and self not in value:
                value.add_person(self)
            self._queue = value
    
    
        def __repr__(self):
            return f'Person({self.name}, {self.age})'
    
    
    
    class Queue(collections.deque):
        def add_person(self, person):
            self.append(person)
            person.queue = self
    
    
    # create 3 instances of Person class
    alice = Person('Alice', 30)
    bob = Person('Bob', 52)
    charlie = Person('Charlie', 40)
    
    queue = Queue()
    queue.add_person(alice)
    bob.queue = queue
    
    print(queue) # both alice and bob are in queue
    print(alice.rank) # this will print 0
    print(charlie.rank) # this will raise ValueError
    
    Queue([Person(Alice, 30), Person(Bob, 52)])
    0
    Traceback (most recent call last):
      File "***", line 53, in <module>
        print(charlie.rank) # this will raise ValueError
      File "***", line 16, in rank
        raise ValueError(f'{self} not in queue')
    ValueError: Person(Charlie, 40) not in queue