How about some syntactic sugar for " __name__ == '__main__' "?
Terry Reedy
tjreedy at udel.edu
Wed Nov 12 18:19:53 EST 2014
More information about the Python-list mailing list
Wed Nov 12 18:19:53 EST 2014
- Previous message (by thread): How about some syntactic sugar for " __name__ == '__main__' "?
- Next message (by thread): How about some syntactic sugar for " __name__ == '__main__' "?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 11/12/2014 4:02 PM, John Ladasky wrote: > I have taught Python to several students over the past few years. As > I have worked with my students, I find myself bothered by the > programming idiom that we use to determine whether a module is being > executed or merely imported: > > "if __name__ == '__main__':" > > The use of two dunder tokens -- one as a name in a namespace, and the > other as a string, is intimidating. It exposes too much of Python's > guts. As such, I think that it is less Pythonic than we might want. > Myself, I've been programming in Python for a decade, and I still > haven't dug very deeply into what exactly __name__ means or does. > > I would like to start a discussion about whether Python should > include a function which executes this evaluation, and hides all of > the unfriendly dunderish details. And if that's a good idea, I would > invite a discussion of how, exactly, it should be implemented. I'm > nowhere near proposing a PEP, but that may come later. Functions have an implicit 'return None' at the end (which, in CPython, become an explicit pair of bytecodes, even when the function already ends with return something'. The simplest proposal is that modules have an implicit "if __name__ == '__main__': main()" at the end. I think this would not have to be added to the bytecode. This magical invocation mimics C and some other languages, and I think it works well. -- Terry Jan Reedy
- Previous message (by thread): How about some syntactic sugar for " __name__ == '__main__' "?
- Next message (by thread): How about some syntactic sugar for " __name__ == '__main__' "?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list