我想將使用items該類的所有變數添加到串列中,而無需在每次創建新專案時手動將每個專案添加到串列中。例如itemsList = [flower, wood, iron, sticks]
flower = items(red, "flower", "0001",xpositions[0] ,ypositions[0])
wood = items(brown, "wood", "0002",xpositions[1] ,ypositions[1])
iron = items(iron, "iron", "0003",xpositions[2] ,ypositions[2])
sticks = items(brown, "sticks", "0004",xpositions[3] ,ypositions[3])
itemsList = []
類的代碼
class items:
def __init__(self, colour, name, ID, xpos, ypos):
self.width = 20
self.height = 20
self.colour = colour
self.name = name
self.ID = ID
self.xpos = xpos
self.ypos = ypos
uj5u.com熱心網友回復:
考慮使用字典而不是松散變數。
my_items = {}
...
my_items["flower"] = items(red, "flower", "0001", xpositions[0], ypositions[0])
my_items["wood"] = items(brown, "wood", "0002", xpositions[1], ypositions[1])
my_items["iron"] = items(iron, "iron", "0003", xpositions[2], ypositions[2])
my_items["sticks"] = items(brown, "sticks", "0004", xpositions[3], ypositions[3])
之后如果你想使用它們中的任何一個,你可以通過my_items字典中的名稱訪問它們,例如my_items["flower"]。
uj5u.com熱心網友回復:
您可以創建一個串列類變數并附self加到__init__. 你能把你班級的代碼貼出來嗎?我也許可以演示一下。
編輯 - 剛剛測驗了這個,我相信這應該是你想要的,雖然我有點不清楚:即
class Item:
list = []
def __init__(self):
Item.list.append(self)
a = Item()
b = Item()
print(Item.list)
然后您可以直接通過類(通過Item.list)或在任何專案中訪問它,但是如果您進行更改,事情會變得很奇怪,因為它在您更改后表現得像一個實體屬性,所以我建議不要使用它。
我看到你說你只想要一個名字串列的評論。我不確定您是否想要相同或專案,但這里是串列中的名稱:
class items:
items_list = []
def __init__(self, colour, name, ID, xpos, ypos):
self.width = 20
self.height = 20
self.colour = colour
self.name = name
self.ID = ID
self.xpos = xpos
self.ypos = ypos
Item.item_list.append(self.name)
更靈活
class items:
items_list = []
def __init__(self, colour, name, ID, xpos, ypos):
self.width = 20
self.height = 20
self.colour = colour
self.name = name
self.ID = ID
self.xpos = xpos
self.ypos = ypos
Item.item_list.append(self)
def __str__(self):
return f'{self.name}'
額外的 str 會做同樣的事情,但實際上存盤專案并列印出專案名稱的串列。
但是您需要明確您是想要專案串列還是專案名稱串列。這些是不同的東西。
uj5u.com熱心網友回復:
您可以使用 locals() 或 globals() 從當前作用域中獲取各個變數名稱。如果您真正想要的是存盤在變數中的實體串列,則可以使用類似的方法。
class items:
...
def variables(scope=globals()):
return [name for name,i in scope.items() if isinstance(i,items)]
def instances(scope=globals()):
return [value for value in scope.values() if isinstance(value,items)]
全球范圍:
gFlower = items(red, "flower", "0001",xpositions[0] ,ypositions[0])
gWood = items(brown, "wood", "0002",xpositions[1] ,ypositions[1])
gIron = items(iron, "iron", "0003",xpositions[2] ,ypositions[2])
gSticks = items(brown, "sticks", "0004",xpositions[3] ,ypositions[3])
print(items.variables())
['gFlower', 'gWood', 'gIron', 'gSticks'] # variable names
print([i.name for i in items.instances()])
['flower', 'wood', 'iron', 'sticks'] # self.name of each instance
區域作用域(例如在函式內部):
def myFunction():
myflower = items(red, "flower", "0001",xpositions[0] ,ypositions[0])
mywood = items(brown, "wood", "0002",xpositions[1] ,ypositions[1])
myiron = items(iron, "iron", "0003",xpositions[2] ,ypositions[2])
mysticks = items(brown, "sticks", "0004",xpositions[3] ,ypositions[3])
print(items.variables(locals()))
myFunction()
['myflower', 'mywood', 'myiron', 'mysticks']
uj5u.com熱心網友回復:
添加itemsList.append到__init__專案類的函式中。
class Item:
itemsList = []
def __init__(self, *args):
self.data = [arg for arg in args]
# This line here is what you want
Item.itemsList.append(self)
uj5u.com熱心網友回復:
這個問題有點不清楚,但你可以嘗試這樣的事情
itemsList = []
itemsList.append(items(red, "flower", "0001",xpositions[0] ,ypositions[0]))
itemsList.append(items(brown, "wood", "0002",xpositions[1] ,ypositions[1]))
itemsList.append(items(iron, "iron", "0003",xpositions[2] ,ypositions[2]))
itemsList.append(items(brown, "sticks", "0004",xpositions[3] ,ypositions[3]))
但是,如果您的意思是它會自動自動添加到串列中,那么這是不可能的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357431.html
