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?

      • Lucy :3
        link
        fedilink
        62
        edit-2
        8 days ago

        Never heard of

        def main():
            pass
        
        if __name__ == '__main__':
            main()
        

        ?

        • @grrgyle@slrpnk.net
          link
          fedilink
          488 days ago

          I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af

          • @frezik@midwest.social
            link
            fedilink
            97 days ago

            Python: I’m so readable that I’m practically executable pseudo-code

            Also Python: if __name__ == '__main__': . . .

          • @bane_killgrind@slrpnk.net
            link
            fedilink
            English
            58 days ago

            Now think about this, you have logic that doesn’t make sense when run directly, but you need it to be a library.

            You have multiple name=main statements in some of your functions

            • @grrgyle@slrpnk.net
              link
              fedilink
              27 days ago

              I’m not sure I’m following the implication. Name=main is for scripts primary, is it not?

              I’ve never thought to add more than one of these conditionals anyway…

              • @bane_killgrind@slrpnk.net
                link
                fedilink
                English
                27 days ago

                So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers.

                So you make it do something slightly different or make it set it’s path and look into the current folder when you run it directly. This change in logic could be in a few points in the script.

          • @Anomalocaris@lemm.ee
            link
            fedilink
            47 days ago

            I still wonder why.

            unless it’s for something that you want to work as an importable module and a standalone tool, then why do you need that?

            • @Archr@lemmy.world
              link
              fedilink
              37 days ago

              The main two reasons that I can think of to include this even when you have no intention of importing this as a library are:

              1. For unit testing you will need to import as a module.
              2. Sometimes I will run a python interactive interpreter and then import my script so that I can do some manual testing without needing to change my main function or if stmt.
            • nickwitha_k (he/him)
              link
              fedilink
              1
              edit-2
              7 days ago

              This is exactly why the conditional is used. It allows the script to function both as a standalone application and a library.

              ETA: Probably would make sense to just treat it as default behavior in the interpreter and only require the conditional to overwrite in cases where main is not the main function and/or pre-processing is needed.

            • @grrgyle@slrpnk.net
              link
              fedilink
              17 days ago

              Oh that is a good point actually. It’s been a while since I have done any serious Python, so I’m not sure why you couldn’t just use convention instead of this conditional.

              For my part, if a Python script is meant to be executed, then I’ll give it a shebang, drop the .py, and simply mark it as executable in the filesystem. 🤷‍♂️

        • @HiddenLayer555@lemmy.mlOP
          link
          fedilink
          English
          22
          edit-2
          8 days ago

          Heard of it, was too lazy to do it that way.

          To be fair I now do it that way, but not when I was learning Python.

          • Lucy :3
            link
            fedilink
            67 days ago

            Not having tons of code in one if statement, but in a function.

            • Ethan
              link
              fedilink
              English
              27 days ago

              I thought you were saying to literally use def main(): pass, that’s why I was confused

              • Lucy :3
                link
                fedilink
                16 days ago

                Oh, no, that’s just the usual placeholder. Though, … would also be valid iirc, and would fit better as a “TODO” placeholder

            • @IronKrill@lemmy.ca
              link
              fedilink
              117 days ago

              And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function.

    • Eager Eagle
      link
      fedilink
      English
      31
      edit-2
      8 days ago

      I work in an academic / research environment. Depending who wrote it, even seeing a __name__ == "__main__" is a bit of a rare thing…

      • ℍ𝕂-𝟞𝟝
        link
        fedilink
        English
        148 days ago

        Do you also have nothing but love for those 50+ cell Jupyter notebooks that don’t use a single function and have everything in the global scope?

        • Eager Eagle
          link
          fedilink
          English
          138 days ago

          the best thing is when not even the author knows the correct order of running the cells; because of course it isn’t top-to-bottom.

          • ℍ𝕂-𝟞𝟝
            link
            fedilink
            English
            108 days ago

            Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.

            And don’t forget the number one rule: you must use all the graphing libraries, all the time.

      • @brian@programming.dev
        link
        fedilink
        37 days ago

        python isn’t the only language to do “execute everything imported from a particular file and all top level statements get run”. both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I’m sure there’s more.

        python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn’t hurt either.

        for instance in node this is the equivalent, even though I’ve never seen someone try before:

        if (path.resolve(url.fileURLToPath(import.meta.url)).includes(path.resolve(process.argv[1])))
        {
          // main things
        }
        
    • @wise_pancake@lemmy.ca
      link
      fedilink
      148 days ago

      Why would you waste a function call on something so completely redundant?

      ~For real though, arg parsing goes in the if, then gets dispatched to whatever function call is needed to run the proper script.~

    • @NeatNit@discuss.tchncs.de
      link
      fedilink
      98 days ago

      I definitely do for quick scripts, but I try to break this habit. The biggest advantage of def main() is that variables are local and not accessible to other functions defined in the same script, which can sometimes help catch bugs or typos.

    • @LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      7
      edit-2
      8 days ago

      If the file is just a class I usually put example usage with some default arguments in that block by itself. There is no reason for a “main” function. It’s a nice obvious block that doesn’t run when someone imports the class but if they’re looking at the class there is a really obvious place to see the class usage. No confusion about what “main()” is meant to do.

      if __name__ == '__main__':
          # MyClass example Usage
          my_object = MyClass()
          my_object.my_method()