1

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
logan9997
  • 73
  • 1
  • 5

5 Answers5

1

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Tyk
  • 68
  • 9
  • Please don't add commentary to your posts. I removed it for you, but if you want, you can comment on [the relevant answer](/a/69980235/4518341) instead. – wjandrea Nov 15 '21 at 20:44
  • I'm not sure what you mean by "adding commentary to posts" but I cannot comment on posts in any case, because of the rules of this website. – Tyk Nov 15 '21 at 20:48
  • Ah yeah, sorry, you aren't able to [comment everywhere](/help/privileges/comment) yet. What I mean is, you wrote *"I see someone else ... answered the same thing ... though they were downvoted for some reason."* Stack Overflow is meant to be a reference site, so stuff like that, which isn't relevant to *your* answer, is [noise](https://meta.stackoverflow.com/a/260778/4518341) and should be removed. BTW, you can look at [the revision history](/posts/69980261/revisions) to see what I changed. – wjandrea Nov 15 '21 at 21:38
  • 1
    Alright, I want to give them some kind credit because I had forgot the exact syntax on class attributes, but I see what you mean. – Tyk Nov 15 '21 at 22:32
1

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']
Alain T.
  • 40,517
  • 4
  • 31
  • 51
1

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"].

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

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)
jozborn
  • 39
  • 1
  • 3
0

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

d3corator
  • 1,154
  • 1
  • 9
  • 23