Efficient python programming...

Daniel Fackrell dfackrell at DELETETHIS.linuxmail.org
Fri Jun 7 17:17:10 CEST 2002


"Shagshag13" <shagshag13 at yahoo.fr> wrote in message
news:adpktk$17j3k$1 at ID-146704.news.dfncis.de...

...

> set = {}
> map(set.__setitem__, alist, [])
> alist = set.keys()
>
> But that's not trivial !!! And quite difficult to find...

The reason that this example does not seem trivial to you or to me is most
likely that we don't fully grasp the concept and usage of map().  Once we
get that behind us, I'm sure that this example will turn into something that
we use all the time without much thinking about it.

>
> > those things doesn't make your code obfuscated..
>
> I agree :)
>
> > [file.printsize() for file in directory]
> >
> > they're the same, and everyone would recommend you the last one..
>
> Yes but i would have asked why this is the best... And i think that
> your answer would have helped me to become a better pythoner...

This concept (I believe it's called "list comprehensions") is another like
the map() usage.  Obscure and arcane to those who don't fully grasp it (like
me, again), but very powerful.  I'm not sure what either of them has to do
with optimization, though, except that they are both probably very well
optimized at the underlying C level.

Daniel Fackrell





More information about the Python-list mailing list