mapLast, mapFirst, and just general iterator questions
Cameron Simpson
cs at cskk.id.au
Tue Jun 14 22:05:37 EDT 2022
On 15Jun2022 05:49, Chris Angelico <rosuav at gmail.com> wrote:
>On Wed, 15 Jun 2022 at 05:45, Roel Schroeven <roel at roelschroeven.net> wrote:
>> 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.
>
>If it does need to be used as a module as well as a script, sure. But
>(a) not everything does, and (b) even then, you don't need a main()
>function; what you need is the name-is-main check. The main function
>is only necessary when you need to be able to invoke your main entry
>point externally, AND this main entry point doesn't have a better
>name. That's fairly rare in my experience.
While I will lazily not-use-a-function in dev, using a function has the
benefit of avoiding accidental global variable use, because assignments
within the function will always make local variables. That is a big plus
for me all on its own. I've used this practice as far back as Pascal,
which also let you write outside-a-function code, and consider it a
great avoider of a common potential bug situation.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list