我有一堂課:
class AlchemicalElement:
def __init__(self, name: str):
self.name = name
然后我創建一個用于存盤 AlchemicalElement 物件的類:
class AlchemicalStorage:
def __init__(self):
self.storage_list = []
我不明白如何撰寫這個函式:
def extract(self) -> list[AlchemicalElement]:
"""Return a list of all the elements from storage and empty the storage itself."""
return []
到目前為止,我只管理了這個:
def add(self, element: AlchemicalElement):
if isinstance(element, AlchemicalElement):
self.storage_list.append(element)
else:
raise TypeError()
這就是我試圖實作的目標:
storage = AlchemicalStorage()
storage.add(AlchemicalElement('Water'))
storage.add(AlchemicalElement('Fire'))
storage.extract() # -> [<AE: Water>, <AE: Fire>]
storage.extract() # -> []
問題
如何撰寫該.extract()方法,以便在第一次呼叫時回傳 中的元素串列storage_list,但在之后的任何呼叫中回傳一個空串列?
uj5u.com熱心網友回復:
假設這是你開始的
class AlchemicalElement:
def __init__(self, name: str):
self.name = name
class AlchemicalStorage:
def __init__(self):
self.storage_list = []
def add(self, element: AlchemicalElement):
if isinstance(element, AlchemicalElement):
self.storage_list.append(element)
else:
raise TypeError()
def extract(self) -> list[AlchemicalElement]:
"""Return a list of all the elements from storage and empty the
storage itself."""
return []
if __name__ == "__main__":
storage = AlchemicalStorage()
storage.add(AlchemicalElement('Water'))
storage.add(AlchemicalElement('Fire'))
print(storage.extract()) # -> [<AE: Water>, <AE: Fire>]
print(storage.extract()) # -> []
您當前將獲得如下所示的輸出
[]
[]
您的第一個問題是如何使.extract()方法耗盡.storage_list?
我們可以這樣做:
def extract(self) -> list[AlchemicalElement]:
"""Return a list of all the elements from storage and empty the
storage itself."""
# copy the storage_list into out_list and then clear it
# only works once!
out_list = self.storage_list.copy()
self.storage_list.clear()
return out_list
現在,如果我們運行腳本,我們應該得到以下資訊:
[<__main__.AlchemicalElement object at 0x000001CB12CBBD00>, <__main__.AlchemicalElement object at 0x000001CB12CBBCA0>]
[]
現在也許要回答另一個問題,我該如何讓它回傳<AE: Water>而不是回傳<__main__.AlchemicalElement object at ...>?
我們可以通過向類添加一個__repr__方法來做到這一點AlchemicalElement
class AlchemicalElement:
def __init__(self, name: str):
self.name = name
def __repr__(self):
return f"<AE: {self.name}>"
所有代碼現在看起來像這樣
class AlchemicalElement:
def __init__(self, name: str):
self.name = name
def __repr__(self):
return f"<AE: {self.name}>"
class AlchemicalStorage:
def __init__(self):
self.storage_list = []
def add(self, element: AlchemicalElement):
if isinstance(element, AlchemicalElement):
self.storage_list.append(element)
else:
raise TypeError()
def extract(self) -> list[AlchemicalElement]:
"""Return a list of all the elements from storage and empty the
storage itself."""
# copy the storage_list into out_list and then clear it
# only works once!
out_list = self.storage_list.copy()
self.storage_list.clear()
return out_list
if __name__ == "__main__":
storage = AlchemicalStorage()
storage.add(AlchemicalElement('Water'))
storage.add(AlchemicalElement('Fire'))
print(storage.extract()) # -> [<AE: Water>, <AE: Fire>]
print(storage.extract()) # -> []
輸出看起來像你想要的
[<AE: Water>, <AE: Fire>]
[]
筆記:
該
__repr__方法應回傳物件的可列印表示,很可能是創建此物件的一種方法。資源
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530871.html
標籤:Python列表哎呀
下一篇:如何比較串列串列中的字串?
