mardi 3 juillet 2018

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

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.

Aucun commentaire:

Enregistrer un commentaire