At 02:12 PM 2/26/04 -0500, Jewett, Jim J wrote: >Jim Jewett: > >> Also, many of them don't work with the proposed > >> syntax. Specifically, you're trying to provide > >> a second binding for the function (such as running > >> for __main__, or registering with the exit handler). > > >> For me, this is the most common use case, but ... > >> PEP 318 doesn't handle it. (Which is one reason > >> that I don't think this will be the *last* extension, > >> which is one reason I don't want *bare* syntax.) > >Bob Ippolito: > > Sorry, but you understate the capabilities of this new > > syntax and overestimate the need for future related syntax. > >Not really; I guess I should have left in the full original >example. > > def foo[__main__]: > >will not work. You mean: def foo()[__main__]: and it will indeed work. Just try it: Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. IDLE 0.8 -- press F1 for help >>> def __main__(f): if f.func_globals['__name__']=='__main__': f() return f >>> def foo(): print "testing" >>> foo = __main__(foo) testing >>> foo <function foo at 0x00A9E990> __main__ could be defined in another module and imported. It's not specific to a particular main function. It simply runs the function if it's defined in the main program module. And in either case, it returns the original function, so that if the module is not "main", then the function is still available for importing. Thus, the 'foo' function would still be the 'foo' function, but it would also be run if it was the main module. The only remaining downside I see is that foo() could not call itself recursively, since it can run before the name 'foo' is bound to it.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4