Constructor problem

Alex Martelli aleaxit at yahoo.com
Fri Oct 29 01:04:03 CEST 2004


Nick <mediocre_person at hotmail.com> wrote:
   ...
> I'm writing a simple class to represent a deck of cards. Here is the 
> stripped down version. Notice classes Deck and Deck2. Class Deck works,
> Deck2 does not. My thinking is that it should be more efficient (a la
> Deck2) to pre-create the list, and then modify it, rather than appending
> Cards as in Deck.

Everybody's accusing you of premature optimization, and they have a
point, but, there IS a very simple approach that's fast and useful
(particularly in Python 2.4 -- if you're interested in performance, get
and use 2.4 beta 1 *NOW*!!!-): a list comprehension:

  self.deck = [Card(r, s) for r in xrange(13) for s in xrange(4)]


Alex



More information about the Python-list mailing list