[Tutor] List methods/comps Best Practices

stv stvsmth at gmail.com
Mon Apr 3 17:26:37 CEST 2006

I just thumped my head against the wall for a few hours on something,
and I was wondering if it's just my green-ness in Python, or if I'm
doing something unsavory.

I had several list comprehensions that I was mucking with; these lists
are working on a simple subclass of the built-in list object. They
looked liked this:

  filelist = getFilesToAdd()
  filelist2 = getFilesToDel()

  adds = MyList('foo')
  dels = MyList('bar')

  [adds.add_changes('foo', path) for path in filelist]
  [dels.add_changes('bar', path) for path in filelist2]

  # return all changes, deletes first
  return dels.extend(adds)

Since extend returns None, I ran into a lot of not-iterable errors
when calling this code. So I fixed this with

  return dels

And all is good, although it took way more head scratching than typing
. (In writing this, I now vaguely remember one of my tutorials warning
me about something ... maybe this was that? :)

So my question is this: Is this just one of those things that I learn
the hard way in Pthon & that's it, or am I doing something weird? I
shoulda figured this out when I was debugging the method in the
interpreter and my original list comprehensions returned

  [adds.add_changes('foo', path) for path in filelist]
  >>> [None, None, None]

as each call to add_changes returned no value. Alas, I'm not that
smart. Is it bad form to not have an "assignment" for the list comps?

More information about the Tutor mailing list