请检查以下示例代码,我想知道版本1是否是在python中使用property
的正确方法。或者我应该遵循版本2模式?或者它们都不正确?
class Queue():
def __init__(self):
self._queue = []
def add(self, val):
self._queue.append(val)
def pop(self):
return self._queue.pop(0)
# version 1
class Peer():
def __init__(self, host, port):
self.host = host
self.port = port
self._job_queue = Queue()
@property
def job_queue(self):
return self._job_queue
if __name__ == '__main__':
peer = Peer('195.12.3.4', '8888')
peer.job_queue.add('some stuff')
peer.job_queue.pop()
# version 2
class Peer():
def __init__(self, host, port):
self.host = host
self.port = port
self._job_queue = Queue()
def add_to_queue(self, val):
self._job_queue.add(val)
def pop_from_queue(self):
return self._job_queue.pop()
if __name__ == '__main__':
peer = Peer('195.12.3.4', '8888')
peer.add_to_queue('some stuff')
peer.pop_from_queue()