Constructor problem

Robert Kern rkern at ucsd.edu
Thu Oct 28 18:09:49 EDT 2004


Nick wrote:

> class Deck2:
>     def __init__(self):
>         self.deck = 52 * [Card()]

Here you are creating a single Card instance and putting the same object 
into a list 52 times. Each slot in the list points to the same object.

Another lesson to take away from this: Don't optimize prematurely. The 
simplest implementation should be fine unless proven otherwise via 
profiling.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list