Also, do y’all call main() in the if block or do you just put the code you want to run in the if block?

  • _____@lemm.ee
    link
    fedilink
    English
    arrow-up
    27
    arrow-down
    1
    ·
    2 days ago

    Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: “Yeah, that is a bit hacky, isn’t it?”

    • Dr. Moose@lemmy.world
      link
      fedilink
      English
      arrow-up
      22
      arrow-down
      4
      ·
      edit-2
      2 days ago

      Tbh reserving “main” is just a hacky if not more so than checking __name__ if you actually understand language design.

      • TheNamlessGuy@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 day ago

        Both are indeed equally terrible, even if it is for different reasons.

        The one true choice is of course letting the programmer choose the main function in compile/interpretation-time.

        I.e. python main.py --start "main" would start by calling the main function in main.py

        • Dr. Moose@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 day ago

          Most contemporary python tools like flask or uvicorn do exactly this and require an explicit entry point

      • namingthingsiseasy@programming.dev
        link
        fedilink
        arrow-up
        8
        ·
        2 days ago

        Reserving main is definitely more hacky. Try compiling multiple objects with main defined into a single binary - it won’t go well. This can make a lot of testing libraries rather convoluted, since some want to write their own main while others want you to write it because require all kinds of macros or whatever.

        On the other hand, if __name__ == "__main__" very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.

      • bastion@feddit.nl
        link
        fedilink
        arrow-up
        10
        arrow-down
        1
        ·
        2 days ago

        Yeah, this is it.

        What’s hacky about an introspective language providing environment to all of the executing code, so that the coder can make the decision about what to do?

        It would by hacky if Python decided “We’ll arbitrarily take functions named “main” and execute them for you, even though we already started execution at the top of the file.”

        For C, this is less so. The body of the file isn’t being executed, it’s being read and compiled. Without a function to act as a starting point, it doesn’t get executed.

          • bitfucker@programming.dev
            link
            fedilink
            arrow-up
            1
            ·
            2 days ago

            I don’t understand. What do you mean by deciding what the code should do in the context of language design? Can you give a concrete example? I am confused because the “main” function is required when you make an executable. Otherwise, a library will not contain any main function and we could compile it just fine no? (Shared library)

            • _stranger_@lemmy.world
              link
              fedilink
              arrow-up
              2
              ·
              2 days ago

              Python is an interpreted language that doesn’t need a main function explicitly. You can define any package entry points you want at the package config level. (setup.py, etc)

              example: What I meant was I prefer language that treat developers like adults. If I want ptrhon’s “ux” to hide some functions or objects I can do that with underscores, but nothing is private, a developer using my library can do whatever they want with it, access whatever internals they want (at their own risk of course)

    • namingthingsiseasy@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      2 days ago

      Is it? I really don’t think so. What can you propose that’s better? I think if __name__ == __main__ works perfectly fine and can’t really think of anything that would be better.

      And you don’t have to use it either if you don’t want to anyway, so no, I don’t think it’s that much of a hack. Especially when the comic compares C as an example, which makes no sense to me whatsoever.