David (dblume) wrote,
David
dblume

Prepopulating lists with objects in Python

Dear self,

Never admit to the world you lost a couple of hours thinking that the following line created a list of objects.

    l = [Obj()] * n


It doesn't. It creates a list of references to one object.

What you meant to write was this:

    l = [Obj() for _ in range(n)]


You dummy.
Subscribe

  • Holiday and Birthday Loot

    Here's a sampling of the loot I got over the holidays. (My birthday occurs just after Christmas.) I can't tell you how much I love that my family…

  • ComicCon '09 Report

    Overview: Sightings/Meetings: Mike Mignola, Camilla D'Errica, David Mack, The Penny Arcade Duo, Josh Howard Far Away Sightings: Peter Jackson,…

  • ComicCon Schedule

    There's no telling how my ComicCon day will actually go down this year. Popular rooms fill up, and squatting through multiple sessions is allowed.…

  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments