[Python-Dev] User's complaints

Bob Ippolito bob at redivi.com
Thu Jul 13 09:58:08 CEST 2006


On Jul 13, 2006, at 12:37 AM, Wolfgang Langner wrote:

> On 7/13/06, Jeroen Ruigrok van der Werven <ashemedai at gmail.com> wrote:
>> Things that struck me as peculiar is the old:
>>
>> if __name__ == "__main__":
>>     whatever()
>>
>> This is so out of tune with the rest of python it becomes a nuisance.
>
> It is not beautiful but very useful.
> In Python 3000 we can replace it with:
>
> @main
> def whatever():
>     ...
>
> to mark this function as main function if module executed directly.

It would probably need to be called something else, because main is  
often the name of the main function... but you could write such a  
decorator now if you really wanted to.

def mainfunc(fn):
     if fn.func_globals.get('__name__') == '__main__':
         # ensure the function is in globals
         fn.func_globals[fn.__name__] = fn
         fn()
     return fn

@mainfunc
def main():
     print 'this is in __main__'

-bob



More information about the Python-Dev mailing list