[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: Creating several instances of same image



On Mon, 26 Jul 2004, Daniel Dornhardt wrote:

> Jasper Phillips wrote:
> 
> >def offset( x, y ):
> >  return (LEFT + x * DISTANCE, TOP + y * DISTANCE)
> >
> >alienList = [Alien(offset(x,y)) for x in range(COLUMNS) for y in range(ROWS)]
> >
> ???
> does it work? :D
> i pseudo-tried it, it works, cool
> it surely makes the whole process clearer and much easier to grok (but 
> not for me...)
> why does it work?

This construct is a part of Python's syntax, called a "list comprehension",
which was added in Python 2.1 or 2.2.
http://www.google.com/search?q=python+list+comprehension

You can also add an if clause to the end of a list comprehension, which is
often usefull.

The syntax takes a bit of getting used to, but in my experience often yields
clearer code.  I just wish there was a clean way to use it for dictionaries!

-Jasper