Skip to content

Equivalent of __name__ from Python? #277

Closed
@thiagokokada

Description

Multiple script languages have an idiom to detect if the current file is being imported by another file or is the main file of the program (in this case, being the main file means that this is the file that started the program).

For example, in Python we have this idiom (an explanation what is happening here can be found in https://stackoverflow.com/a/419185):

if __name__ == "__main__":
    main()

This is useful since it allows the file to be imported by other files, however at the same time it allows the file to be executed directly. This doesn't make a lot of sense in compiled languages like Clojure (since they only have one entrypoint), however in script languages they make more sense since you can basically execute any as a full program.

Having something like a variable called *name* for example would allow for something like:

(when (= *name* "__main__")
  (main))

Btw, this is common enough that Hy for example has a (defmain) to be used in those cases: https://docs.hylang.org/en/stable/language/api.html#defmain. However this may be too much.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions