Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80874
| From | Vito De Tullio <vito.detullio@gmail.com> |
|---|---|
| Subject | Re: How about some syntactic sugar for " __name__ == '__main__' "? |
| Date | 2014-11-16 11:39 +0100 |
| References | <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> <CAPM-O+yukMKq8m1Udcw23=kgPc6zvncfS1ohPcyme_4RXMTATg@mail.gmail.com> <CALvWhxuo8EB_FqkeUXe9nRj1CGFxrVtzw76afc21cB5=D4aZwg@mail.gmail.com> <CALwzid=i6vKUnQU1pfO3qs8H5u+_J4ff_XdkQUxt4ajN5JNYfQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15883.1416134390.18130.python-list@python.org> (permalink) |
Ian Kelly wrote:
>> def main(func):
>> if func.__module__ == "__main__":
>> func()
>> return func # The return could be omitted to block the function from
>> being manually called after import.
>>
>> Just decorate the "main" function of the script with that, and it will be
>> automatically called when ran as a script, but not when imported as a
>> module.
>
> This calls it at the wrong time, though. Typically the way this idiom
> is used is that you define everything you need (functions, classes,
> etc.) within the main script, and then you call the main function.
> This would call the main function at the time it's defined, when other
> things in the main script may not have been defined yet. One could
> place the main function last, but it would be preferable not to be
> forced.
for the "right time" you can choose to spin a thread and wait to the end of
the load of the module
something like
from threading import Thread, current_thread
def run_func(func, module_thread):
module_thread.join()
func()
def main(func):
if func.__module__ == '__main__':
Thread(target=run_func, args=[func, current_thread()]).start()
return func
--
By ZeD
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How about some syntactic sugar for " __name__ == '__main__' "? John Ladasky <john_ladasky@sbcglobal.net> - 2014-11-12 13:02 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-12 16:07 -0500
Re: How about some syntactic sugar for " __name__ == '__main__' "? John Ladasky <john_ladasky@sbcglobal.net> - 2014-11-12 13:17 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-11-12 13:19 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Kaynor <ckaynor@zindagigames.com> - 2014-11-12 13:33 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Marko Rauhamaa <marko@pacujo.net> - 2014-11-12 23:52 +0200
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-12 14:51 -0700
Re: How about some syntactic sugar for " __name__ == '__main__' "? Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-12 15:55 -0600
Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Kaynor <ckaynor@zindagigames.com> - 2014-11-12 14:09 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-15 22:09 +1100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Marko Rauhamaa <marko@pacujo.net> - 2014-11-15 13:54 +0200
Re: How about some syntactic sugar for " __name__ == '__main__' "? Vito De Tullio <vito.detullio@gmail.com> - 2014-11-16 11:26 +0100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-12 15:19 -0700
Re: How about some syntactic sugar for " __name__ == '__main__' "? Terry Reedy <tjreedy@udel.edu> - 2014-11-12 18:19 -0500
Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-11-13 10:26 +1100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Terry Reedy <tjreedy@udel.edu> - 2014-11-12 19:35 -0500
Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-11-13 11:54 +1100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-11-13 10:32 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-13 13:33 -0700
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-11-13 12:43 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-13 14:44 -0600
Re: How about some syntactic sugar for " __name__ == '__main__' "? Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-13 14:53 -0600
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-13 15:48 -0700
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-13 15:56 -0700
Re: How about some syntactic sugar for " __name__ == '__main__' "? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-15 21:52 +1100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Vito De Tullio <vito.detullio@gmail.com> - 2014-11-16 11:39 +0100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-16 10:15 -0700
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-12-01 15:19 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-12-01 16:45 -0800
Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-12-02 12:15 +1100
Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-12-02 02:17 -0800
csiph-web