Home > front end >  Add all class variables to a list
Add all class variables to a list

Time:11-16

I would like to add all variables that use the items class to a list without adding each item manually to a list each time i create a new item. For example 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 = []

code for the class

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

CodePudding user response:

Consider using a dictionary instead of loose variables.

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])

and later if you want to use any of them, you access them by name in the my_items dictionary, for example my_items["flower"].

CodePudding user response:

You could create a list class variable and append self to the class variable in __init__. Can you post the code for your class? I might be able to demonstrate.

Edit- Just tested this out, this should be what you're going for I believe, though I'm a bit unclear: ie

class Item:
    list = []
    def __init__(self):
        Item.list.append(self)

a = Item()
b = Item()
print(Item.list)

Then you can access it via the class directly (via Item.list), or in any of the items, though if you make changes things get weird as it behaves like a instance attribute after you change so I recommend against it.

I see the comment you say you just want a list of names. I'm not sure if you want the sames or the items, but here is the names in the 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)

More flexible

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}'

That extra str will do the same thing but actually store the items AND print out a list of the names of the items.

But you need to clarify if you want the list of items or the list of the names of the items. Those are different things.

CodePudding user response:

You can get the individual variable names from the curent scope using locals() or globals(). A similar approach can be used if what you actually want is the list of instances that are stored in variables.

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)]

Global scope:

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

Local scope (e.g. inside a function):

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']

CodePudding user response:

Add itemsList.append into your __init__ function for the items class.

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)

CodePudding user response:

The question is a bit unclear but you could try something like this

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]))

But if you mean that it automatically gets added to a list on its own, then that won't be possible

  • Related