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?

  • JATth@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    8 days ago

    I would put my code in a def main(), so that the local names don’t escape into the module scope:

    if __name__ == '__main__':
        def main():
            print('/s')
        main()
    

    (I didn’t see this one yet here.)

    • YourShadowDani@lemm.ee
      link
      fedilink
      English
      arrow-up
      0
      ·
      8 days ago

      I’m a little new to Python standards. Is this better or worse than putting the def main(): outside the if statement (but calling main() inside it)

      • JATth@lemmy.world
        link
        fedilink
        English
        arrow-up
        0
        ·
        8 days ago

        I intended this an sarcastic example; I think it’s worse than putting the main outside of the branch because of the extra indent-level. It does have an upside that the main() doesn’t exist if you try import this as an module.

    • FooBarrington@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      9 days ago

      Python has a bunch of magic variables, like __name__. This one contains the name of the module you’re currently in (usually based on the file name), so if your file is called foo.py, it will have the value foo.

      But that’s only if your module is being imported by another module. If it’s executed directly (e.g. python foo.py), it will instead have a __name__ of __main__. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions.

    • HiddenLayer555@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      9 days ago

      Basically, when you compile a program written in Rust or C/C++ (the first and second panels respectively), the compiler needs to know what’s supposed to be executed first when the program is run directly (i.e. when you click on the executable), which in these languages, is denoted by a special function called main(). Executable files can also contain functions and data structures that can be called by other programs, and when they are, you wouldn’t want to run an entire complex and resource intensive program if another program only needs to call a single function from it, so in that case the other program will call the function it wants but not main, so only that function executes and not the entire program.

      However, Python is a scripting language that’s interpreted. So every Python source file is executable provided you have the Python runtime. Python also doesn’t have native support for main functions in the same way Rust and C/C++ does, and it will execute every line of code as it reads the source file. This is why a single line Python file that just calls print is valid, it doesn’t need to be wrapped in a main function to execute. However, what if your Python file is both meant to be executed directly and provides functions that other Python files can call? If you just put the main routine in the root of the file, it would be executed every time another program tries to import the file in order to call functions from it, since the import causes the file to be interpreted and executed in its entirety. You can still just have a main function in your file, but since Python doesn’t natively support it, your main function won’t do anything if you run the file directly because as far as Python is concerned, there is no executable code at the root of the file and you haven’t called any functions.

      The workaround is to have a single if statement at the root of the file that looks like this:

      if __name__ == '__main__':
          main()
      

      It checks a special variable called __name__. If the Python file is directly executed, __name__ will have the value of the string '__main__', which satisfies the if statement so main() is called. If another Python file is calling a function, the value of __name__ will be the name of that file, so main() is not called. It’s clunky and not that efficient, but, 1, it works, and 2, if you cared about efficiency, you wouldn’t be writing it in Python.

    • bastion@feddit.nl
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      8 days ago

      All code needs to have an entry point.

      For Python and some other languages, this is the start of the file.

      For other languages, this is a special function name reserved for this purpose - generally, “main”.

      In the first kind of language, the thought process is basically: I have the flow of execution, starting at the top of the file. If I want to make a library, I should build the things I want to build, then get out of the way.

      In the other kind of language, the thought process is basically: I am building a library. If I want to make an executable, I should create an entry point they the execution starts at.

      The debate is honestly pretty dumb.

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

        Python doesn’t need the name main check to function at all. that’s just a convenience feature that lets developers also include arbitrary entry points into modules that are part of a library and expected to be used as such. If you’re writing a script, a file with a single line in it reading print("hello world") will work fine when run: python thescript.py

    • jacksilver@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      9 days ago

      The point of the name==main logic is that it checks if that is the file that was invoked (like running python filename.py). If you just put a main() in the global scope it will be called either when the file is invoked or loaded (which can cause unintended consequences).

      • littlewonder@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        8 days ago

        Dumb person question: if it’s good practice to do this so things don’t go sideways, shouldn’t it be a built-in feature/utility/function/whatever?

        • jacksilver@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          8 days ago

          It is “built-in” as the name is part of python. However, Python runs top to bottom, rather than having a special entrypoint. So name is just a utility you can use in your design.

          While it can be a good practice to define a main entrypoint, that’s more of a design decision and not hard rule. Many applications would not benefit from it because there is only one way to actually call the application to begin with.

          Edit: Also not a dumb question. All programming languages have unique elements to them due to how they were envisioned and or developed over time (Pythons 30 years old)

  • driving_crooner@lemmy.eco.br
    link
    fedilink
    arrow-up
    0
    ·
    8 days ago

    Does everyone call the function of the script main? I never use main(), just call the function what the program is supposed to do, this program calculates the IBNR? The function is called calculate_IBNR(), then at the end of the script if name = ‘main’: calculate_IBNR(test_params) to test de script, then is imported into a tkinter script to be converter to an exe with pyinstaller

    • Whelks_chance@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      7 days ago

      All of mine are called do_thing() because after a few days of working on it, the scope creep always means the original name was wrong anyway.

  • jjjalljs@ttrpg.network
    link
    fedilink
    arrow-up
    0
    ·
    9 days ago

    Call the function from the if block.

    Now your tests can more easily call it.

    I think at my last job we did argument parsing in the if block, and passed stuff into the main function.

    • Eager Eagle@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      9 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…

      • SpaceNoodle@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        9 days ago

        Academic code is absolutely horrific.

        Fortunately, it is possible to translate it for practical applications.

      • ℍ𝕂-𝟞𝟝@sopuli.xyz
        link
        fedilink
        English
        arrow-up
        0
        ·
        9 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@lemmy.world
          link
          fedilink
          English
          arrow-up
          0
          ·
          9 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.

          • ℍ𝕂-𝟞𝟝@sopuli.xyz
            link
            fedilink
            English
            arrow-up
            0
            ·
            9 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
        arrow-up
        0
        ·
        8 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
        }
        
            • IronKrill@lemmy.ca
              link
              fedilink
              arrow-up
              0
              ·
              8 days ago

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

        • grrgyle@slrpnk.net
          link
          fedilink
          arrow-up
          0
          ·
          9 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

          • bane_killgrind@slrpnk.net
            link
            fedilink
            English
            arrow-up
            0
            ·
            9 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
              arrow-up
              0
              ·
              9 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
                arrow-up
                0
                ·
                8 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
            arrow-up
            0
            ·
            8 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
              arrow-up
              0
              ·
              8 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.
          • frezik@midwest.social
            link
            fedilink
            arrow-up
            0
            ·
            8 days ago

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

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

        • HiddenLayer555@lemmy.mlOP
          link
          fedilink
          English
          arrow-up
          0
          ·
          edit-2
          9 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.

    • LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      9 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()
      
    • NeatNit@discuss.tchncs.de
      link
      fedilink
      arrow-up
      0
      ·
      9 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.

    • wise_pancake@lemmy.ca
      link
      fedilink
      arrow-up
      0
      ·
      9 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.~

  • MTK@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    9 days ago

    It really doesn’t. It’s a scripting language, functions are there but at it’s core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts

    It is the excel of databases.

    • Anomalocaris@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      8 days ago

      compared with other languages at the time, the ease of access and readability makes it worth it. plus, the heavy duty stuff is usually handled by more optimised code line numpy or sklearn…

      • Shanmugha@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        8 days ago

        Readability? Me eyes bleed from a day of partially staring at python code, and there is a whole another week of that ahead. Tzinch (Edit: Tzeentch) help me

        • Treczoks@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          8 days ago

          Like in every programming language, it depends who wrote the code. OK, *nearly every programming language, see: LISP.

          You can write cryptic, write-only programs in about any language, but you can even write readable and maintainable PERL scripts (despite people claiming this to be impossible).

          • Shanmugha@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            8 days ago

            As much as I am inclined to agree with this, still can’t

            see: LISP

            Also, see: Python with more than three lines of logic. I could suspect that’s just the me-versus-whitespaces thing, but no, YAML files do not get me dizzy in under thirty seconds of reading. Van Rossum made a huge miscalculation here

            • PolarKraken@programming.dev
              link
              fedilink
              English
              arrow-up
              0
              ·
              8 days ago

              Everyone’s welcome to their opinion of course, but I find Python more readable than anything else and I resent the visual clutter required to make intentions plain in other languages. Feels like having a conversation where people say the words “comma”, “period”, etc.

              I also spend more time with Python than anything else and I suspect these two facts about me relate, lol

              • Shanmugha@lemmy.world
                link
                fedilink
                arrow-up
                0
                ·
                edit-2
                8 days ago

                Someone should get their hands on someone like me and someone like you and study their brains. I spend most time with PHP and C++, and Python looks like an attempt to write code like prose literature. Very interesting how much of this is habbit, as it can’t be just that: reading prose and poetry in English/Russian/Japanese never produced this kind of resentment

                • PolarKraken@programming.dev
                  link
                  fedilink
                  English
                  arrow-up
                  0
                  ·
                  edit-2
                  6 days ago

                  I would love that! I do think there are probably interesting underlying personality factors / preferences for a lot of this stuff as well.

                  I do think that many of Python’s characteristics map to my own personality and I bet there’s something to that. Things like syntax of course, but not strictly syntax, also things like “The Zen of Python”, and the way its a “jack-of-all-trades, master-of-none”. I also really kind of need the freedom and accompanying responsibility to break any “rules” on a whim (Python will happily let you overwrite its own internals while running, for instance), but I almost never do anything that uses it…

                  I could probably keep going lol. Feels like a “people looking like their pets” scenario, lmao

      • jenesaisquoi@feddit.org
        link
        fedilink
        English
        arrow-up
        0
        ·
        8 days ago

        A scripting language controls an existing binary. A non-scripting language is used to create a new binary.

      • mmddmm@lemm.ee
        link
        fedilink
        arrow-up
        0
        ·
        8 days ago

        It’s a scripting language. What means that the computer runs it line by line, without needing to get the entire project first.

        • frezik@midwest.social
          link
          fedilink
          arrow-up
          0
          ·
          8 days ago

          That is not how Python works. There are very few languages that work by executing line-by-line anymore. Unix shell scripts are one of the few holdouts. JavaScript also does it to a certain extent; the browser starts executing line-by-line while a compiler step works in the background. Once the compiler is done, it starts execution of the compiled form right where the line-by-line execution left off. It helps JavaScript be more responsive since it doesn’t have to wait for the compiler to finish.

          • mmddmm@lemm.ee
            link
            fedilink
            arrow-up
            0
            ·
            8 days ago

            Python still has the -i option, and it still runs the same language as the files interface.

          • fruitcantfly@programming.dev
            link
            fedilink
            arrow-up
            0
            ·
            edit-2
            8 days ago

            Unix shell scripts are one of the few holdouts.

            I don’t know if this applies to other shells, but bash will not only execute your script line-by-line, it will also read it line-by-line. Which means that you can modify the behavior of a running script by editing lines that have not yet been executed*. It’s absolutely bonkers, and I’m sure that it has caused more than one system failure, during upgrades.

            * For example, if you run the following script

            echo "hello"
            sleep 5
            echo "goodbye"
            

            and then edit the third line before the 5 second sleep has elapsed, then the modified line will be executed.

      • MTK@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        8 days ago

        I didn’t say it wasn’t real, it’s just a scripting structure and not object oriented, so it doesn’t make sense for it to start by looking for a “main” object

        • mmddmm@lemm.ee
          link
          fedilink
          arrow-up
          0
          ·
          8 days ago

          not object oriented

          I don’t think we have a name for what you are trying to say here.

          (And yeah, “object oriented” isn’t it.)

            • frezik@midwest.social
              link
              fedilink
              arrow-up
              0
              ·
              edit-2
              8 days ago

              Procedural and OOP aren’t mutually exclusive terms. Most OOP programs are ultimately procedural in nature. Often, the only difference is that the first argument to the function is to the left the function name and separated by a dot.

              • AllHailTheSheep@sh.itjust.works
                link
                fedilink
                arrow-up
                0
                ·
                8 days ago

                fair, I just think it’s misleading to call python procedural, but it lines up with what the commenter above was describing and searching for the term for

                • frezik@midwest.social
                  link
                  fedilink
                  arrow-up
                  0
                  ·
                  8 days ago

                  I’d say the term “procedural” itself is an issue. Pretty much any language can be done that way if you choose. IIRC, the creator of Clojure wanted Java to work more that way, and he did it by having a single class full of functions. It’s not a natural way to write Java, and that’s why he invented Clojure.

      • JackbyDev@programming.dev
        link
        fedilink
        English
        arrow-up
        0
        ·
        8 days ago

        Scripting languages are real. Generally people consider dynamic languages scripting languages but it’s not that simple.