Do the `if __name__ == “__main__”: ` like idioms have a name of design pattern?

  • A+
Category:Languages

Do these following idioms (to write a module which is also an executable/runnable) have a name of design pattern?

In Python, we can write a module as an executable too with if name == 'main': idiom:

if __name__ == "__main__":     main() 

Similar idiom can be found in Ruby:

if __FILE__ == $0   main() end 

Also same effect can be achieved differently in Perl too:

main() unless caller; 

In Tcl, you may write:

if {![info level] && [info script] eq $::argv0} {     main } 

Although these are implemented in different ways, they share the same goal: make single script file both a module and an executable/runnable. It seems to me a design pattern. How do you call them? I personally have been called them as Executable Module or Runnable Module, but I want to know the more common name if it exists.

 


In Perl, this pattern is known as a modulino. I believe the term was coined by brian d foy in his book Mastering Perl. I don't often see the name applied for languages other than Perl, but it does happen.

Edit to add: the name goes back earlier than that. Here's an article from 2004 that uses the term.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: