mapLast, mapFirst, and just general iterator questions

Roel Schroeven roel at roelschroeven.net
Tue Jun 14 15:44:26 EDT 2022


Chris Angelico schreef op 14/06/2022 om 20:47:
> > def main():
> >     for each in (iterEmpty, iter1, iter2, iterMany):
> >         baseIterator = each()
> >         chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
> >         andCapLast = mapLast(chopFirst, lambda x: x.upper())
> >         print(repr(" ".join(andCapLast)))
>
> Don't bother with a main() function unless you actually need to be
> able to use it as a function. Most of the time, it's simplest to just
> have the code you want, right there in the file. :) Python isn't C or
> Java, and code doesn't have to get wrapped up in functions in order to
> exist.
Not (necessarily) a main function, but these days the general 
recommendation seems to be to use the "if __name__ == '__main__':" 
construct, so that the file can be used as a module as well as as a 
script. Even for short simple things that can be helpful when doing 
things like running tests or extracting docstrings.

-- 
"This planet has - or rather had - a problem, which was this: most of the
people living on it were unhappy for pretty much of the time. Many solutions
were suggested for this problem, but most of these were largely concerned with
the movement of small green pieces of paper, which was odd because on the whole
it wasn't the small green pieces of paper that were unhappy."
         -- Douglas Adams



More information about the Python-list mailing list