-1

I recently looked up this topic but dint find a satisfying answer. Everybody said using dictionaries is the best way , but I don’t know how to apply that in this case:

I want a function that generates me a list of objects generated by a class. So in pseudo-code something like :

for a in range(100):
    tmp = "object" + str(a)
    var(tmp) = someclass()
    list add tmp

Edit , because of being marked as an duplicate.

I don’t want an answer how dictionary work , I know that. I want to know , how I can make entries which consists of an entry-name and the generated objects for that name. I need a dictionary which looks something like: {"1" : obj1 , "2" : obj2, "3" : obj3 ... }

Edit two: I ended up using a list instead of a dictionary which is massively easier:

    for tmp in range(100):
        tmpobj = objclass()
        list.append(tmpobj)

thanks for help ;)

Pixdigit
  • 80
  • 1
  • 11

1 Answers1

0

You can use a dictionary comprehension :

{ var("object" + str(a)) : someclass() for a in range(100) }
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • I know the problem here is the var() function , which doesn’t exists. But still thanks for helping ;) – Pixdigit Jan 30 '15 at 23:48
  • @MaxLange you're welcome ! if you found this answer helpful you can tell this to community with vote or accept ! – Mazdak Jan 31 '15 at 09:18