0

I have a question about pyglet image resource definition.

If I have lot of pictures can I describe them in a dictionary and make images with for loop?

Fast way:

images = {'pic1':'pic1.jpg',
          'pic2':'pic2.jpg',
          ...}

for (name, val) in images.items():
    # this is correct code
    name = pyglet.resource.image(val)

Normal way:

image1 = pyglet.resource.image("image1.jpg")
image2 = pyglet.resource.image("image2.jpg")
...

With other words I want to use 'name' as name of variable and declare this variable like playget.resource.image(val), where val is name of image.

2 Answers2

2

The following would be more efficient use of Dictionary Comprehension and the generator method iteritems (if you use python 2.x):

{key: pyglet.resource.image(value) for k,v in images.iteritems()}

or if you have a naming pattern ('pic1' => 'pic1.jpg' etc):

{'pic%s' % index: pyglet.resource.image('pic%s.jpg' % index) for index in xrange(1, 1000)}
Community
  • 1
  • 1
astreal
  • 3,383
  • 21
  • 34
  • I edit my question. when i use the first row of code i get an error: "super(Player, self).__init__(img=resources.img1, *args, **kwargs) AttributeError: 'module' object has no attribute 'img1'" – user3186945 Jan 12 '14 at 18:56
  • Can you post your code (or part of it) of the Player and the resources? – astreal Jan 12 '14 at 20:39
0

I think find the solution, may be is not the best, python is not my strong side, and if anyone have better approach let share it!
So this work for me:

for (key,value) in images.iteritems():
   vars()[key] = pyglet.resource.image(value)