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?

  • Laurel Raven@lemmy.zip
    link
    fedilink
    English
    arrow-up
    39
    ·
    7 days ago

    One thing I really dislike about Python is the double underscore thing, just really looks ugly to me and feels excessive. Just give me my flow control characters that aren’t whitespace

    • atx_aquarian@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      7 days ago

      I’m at peace with balanced underscores (like “dunder name equals dunder main”) and the internal ones for snake case, but in the unbalanced ones (prefixing unders and dunders for pseudo-private) still bug me. But at least, conventionally, it’s visually the same idea as Hungarian notation.

  • JATth@lemmy.world
    link
    fedilink
    English
    arrow-up
    18
    ·
    7 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
      5
      ·
      7 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
        5
        ·
        7 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.

  • MTK@lemmy.world
    link
    fedilink
    arrow-up
    32
    arrow-down
    10
    ·
    edit-2
    7 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.

      • jenesaisquoi@feddit.org
        link
        fedilink
        English
        arrow-up
        3
        ·
        6 days ago

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

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

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

      • mmddmm@lemm.ee
        link
        fedilink
        arrow-up
        0
        arrow-down
        7
        ·
        7 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
          13
          ·
          7 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.

          • fruitcantfly@programming.dev
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            6 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.

          • mmddmm@lemm.ee
            link
            fedilink
            arrow-up
            1
            arrow-down
            6
            ·
            7 days ago

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

      • MTK@lemmy.world
        link
        fedilink
        arrow-up
        4
        arrow-down
        11
        ·
        7 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
          10
          ·
          7 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
              4
              ·
              edit-2
              7 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
                2
                ·
                7 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
                  2
                  ·
                  7 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.

    • Anomalocaris@lemm.ee
      link
      fedilink
      arrow-up
      6
      arrow-down
      1
      ·
      7 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
        4
        arrow-down
        3
        ·
        edit-2
        7 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
          4
          arrow-down
          1
          ·
          6 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
            2
            arrow-down
            4
            ·
            6 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
              4
              ·
              6 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
                2
                arrow-down
                1
                ·
                edit-2
                6 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
                  1
                  ·
                  edit-2
                  4 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

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

      Luckily Python is one step ahead:

      Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> if __name__ = "__main__":
      ... 
      ...    main()
      ...    
          File "<python-input-0>", line 1
          if __name__ = "__main__":
              ^^^^^^^^^^^^^^^^^^^^^
      SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
      

      Also TIL that := is a thing in Python.

      • iAvicenna@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        5 days ago

        yea I also couldnt get the formatting to work right, triple quotes kept turning things into accented letters, so I gave up.

        and also := also known as the walrus operator is very fun and sometimes very convenient to use

  • driving_crooner@lemmy.eco.br
    link
    fedilink
    arrow-up
    6
    ·
    6 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
      3
      ·
      6 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.

  • 10001110101@lemm.ee
    link
    fedilink
    English
    arrow-up
    3
    ·
    6 days ago

    I’ve always found needing to manually add a class instance parameter (i.e. self) to every object method really weird. And the constructors being named __init__. Not having multiple dispatch is kinda annoying too. Needing to use decorators for class methods, static methods, and abstract classes is also annoying. Now that I think about it, Python kinda sucks (even though it’s the language I use the most, lol).

    • sebsch@discuss.tchncs.de
      link
      fedilink
      arrow-up
      3
      ·
      6 days ago

      Nah self is quite important. The main part of a method is to access the state of the object. self is just the interface to it.

      • 10001110101@lemm.ee
        link
        fedilink
        English
        arrow-up
        4
        ·
        5 days ago

        Guess I just prefer languages that do it this way:

        class AClass {
          var aProp = 0
        
          fun aMethod() {
            aProp++
          }
        }
        

        Though I suppose confusion and bugs can happen when you do something like:

        class AClass {
          var aProp = 0
        
          fun aMethod(aProp: Int) {
            // `this.aProp` is needed to access the property
          }
        }
        
  • onlinepersona@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    7 days ago

    Can someone explain to me how to compile a C library with “main” and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute “main”? Is it possible to have a library that can be called and also executed like a program?

    Anti Commercial-AI license

    • barsoap@lemm.ee
      link
      fedilink
      arrow-up
      20
      ·
      edit-2
      7 days ago

      How does executing a program actually work?

      Way too long an answer for a lemmy post

      It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file?

      Depends on OS. Linux will look at the first bytes of the file, either see (ASCII) #! (called a shebang) or ELF magic, then call the appropriate interpreter with the executable as an argument. When executing e.g. python, it’s going to call /usr/bin/env with parameters python and the file name because the shebang was #!/usr/bin/env python.

      How does it know to execute “main”?

      Compiled C programs are ELF so it will go through the ELF header, figure out which ld.so to use, then start that so that it will find all the libraries, resolve all dynamic symbols, then do some bookkeeping, and jump to _start. That is, it doesn’t: main is a C thing.

      Is it possible to have a library that can be called and also executed like a program?

      Absolutely. ld.so is an example of that.. Actually, wait, I’m not so sure any more, I’m getting things mixed up with libdl.so. In any case ld.so is an executable with a file extension that makes it look like a library.

      EDIT: It does work. My (GNU) libc spits out version info when executed as an executable.

      If you want to start looking at the innards like that I would suggest starting here: Hello world in assembly. Note the absence of a main function, the symbol the kernel actually invokes is _start, the setup necessary to call a C main is done by libc.so. Don’t try to understand GNU’s libc it’s full of hystarical raisins I would suggest musl.

      • onlinepersona@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        7 days ago

        EDIT: It does work. My (GNU) libc spits out version info when executed as an executable.

        How does that work? There must be something above ld.so, maybe the OS? Because looking at the ELF header, ld.so is a shared library “Type: DYN (Shared object file)”

        $ readelf -hl ld.so
        ELF Header:
          Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 
          Class:                             ELF64
          Data:                              2's complement, little endian
          Version:                           1 (current)
          OS/ABI:                            UNIX - GNU
          ABI Version:                       0
          Type:                              DYN (Shared object file)
          Machine:                           Advanced Micro Devices X86-64
          Version:                           0x1
          Entry point address:               0x1d780
          Start of program headers:          64 (bytes into file)
          Start of section headers:          256264 (bytes into file)
          Flags:                             0x0
          Size of this header:               64 (bytes)
          Size of program headers:           56 (bytes)
          Number of program headers:         11
          Size of section headers:           64 (bytes)
          Number of section headers:         23
          Section header string table index: 22
        
        Program Headers:
          Type           Offset             VirtAddr           PhysAddr
                         FileSiz            MemSiz              Flags  Align
          LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
                         0x0000000000000db8 0x0000000000000db8  R      0x1000
          LOAD           0x0000000000001000 0x0000000000001000 0x0000000000001000
                         0x0000000000029435 0x0000000000029435  R E    0x1000
          LOAD           0x000000000002b000 0x000000000002b000 0x000000000002b000
                         0x000000000000a8c0 0x000000000000a8c0  R      0x1000
          LOAD           0x00000000000362e0 0x00000000000362e0 0x00000000000362e0
                         0x0000000000002e24 0x0000000000003000  RW     0x1000
          DYNAMIC        0x0000000000037e80 0x0000000000037e80 0x0000000000037e80
                         0x0000000000000180 0x0000000000000180  RW     0x8
          NOTE           0x00000000000002a8 0x00000000000002a8 0x00000000000002a8
                         0x0000000000000040 0x0000000000000040  R      0x8
          NOTE           0x00000000000002e8 0x00000000000002e8 0x00000000000002e8
                         0x0000000000000024 0x0000000000000024  R      0x4
          GNU_PROPERTY   0x00000000000002a8 0x00000000000002a8 0x00000000000002a8
                         0x0000000000000040 0x0000000000000040  R      0x8
          GNU_EH_FRAME   0x0000000000031718 0x0000000000031718 0x0000000000031718
                         0x00000000000009b4 0x00000000000009b4  R      0x4
          GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                         0x0000000000000000 0x0000000000000000  RW     0x10
          GNU_RELRO      0x00000000000362e0 0x00000000000362e0 0x00000000000362e0
                         0x0000000000001d20 0x0000000000001d20  R      0x1
        

        The program headers don’t have interpreter information either. Compare that to ls “Type: EXEC (Executable file)”.

        $ readelf -hl ls
        ELF Header:
          Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
          Class:                             ELF64
          Data:                              2's complement, little endian
          Version:                           1 (current)
          OS/ABI:                            UNIX - System V
          ABI Version:                       0
          Type:                              EXEC (Executable file)
          Machine:                           Advanced Micro Devices X86-64
          Version:                           0x1
          Entry point address:               0x40b6e0
          Start of program headers:          64 (bytes into file)
          Start of section headers:          1473672 (bytes into file)
          Flags:                             0x0
          Size of this header:               64 (bytes)
          Size of program headers:           56 (bytes)
          Number of program headers:         14
          Size of section headers:           64 (bytes)
          Number of section headers:         32
          Section header string table index: 31
        
        Program Headers:
          Type           Offset             VirtAddr           PhysAddr
                         FileSiz            MemSiz              Flags  Align
          PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                         0x0000000000000310 0x0000000000000310  R      0x8
          INTERP         0x00000000000003b4 0x00000000004003b4 0x00000000004003b4
                         0x0000000000000053 0x0000000000000053  R      0x1
          LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                         0x0000000000007570 0x0000000000007570  R      0x1000
          LOAD           0x0000000000008000 0x0000000000408000 0x0000000000408000
                         0x00000000000decb1 0x00000000000decb1  R E    0x1000
          LOAD           0x00000000000e7000 0x00000000004e7000 0x00000000004e7000
                         0x00000000000553a0 0x00000000000553a0  R      0x1000
          LOAD           0x000000000013c9c8 0x000000000053d9c8 0x000000000053d9c8
                         0x000000000000d01c 0x0000000000024748  RW     0x1000
          DYNAMIC        0x0000000000148080 0x0000000000549080 0x0000000000549080
                         0x0000000000000250 0x0000000000000250  RW     0x8
          NOTE           0x0000000000000350 0x0000000000400350 0x0000000000400350
                         0x0000000000000040 0x0000000000000040  R      0x8
          NOTE           0x0000000000000390 0x0000000000400390 0x0000000000400390
                         0x0000000000000024 0x0000000000000024  R      0x4
          NOTE           0x000000000013c380 0x000000000053c380 0x000000000053c380
                         0x0000000000000020 0x0000000000000020  R      0x4
          GNU_PROPERTY   0x0000000000000350 0x0000000000400350 0x0000000000400350
                         0x0000000000000040 0x0000000000000040  R      0x8
          GNU_EH_FRAME   0x0000000000126318 0x0000000000526318 0x0000000000526318
                         0x0000000000002eb4 0x0000000000002eb4  R      0x4
          GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                         0x0000000000000000 0x0000000000000000  RW     0x10
          GNU_RELRO      0x000000000013c9c8 0x000000000053d9c8 0x000000000053d9c8
                         0x000000000000c638 0x000000000000c638  R      0x1
        

        It feels like somewhere in the flow there is the same thing that’s happening in python just more hidden. Python seems to expose it because a file can be a library and an executable at the same time.

        Anti Commercial-AI license

        • barsoap@lemm.ee
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          7 days ago

          Your ld.so contains:

          Entry point address: 0x1d780

          EDIT: …with which I meant, modulo brainfart: My libc.so.6 contains a proper entry address, while other libraries are pointing at 0x0 and coredump when executed. libc.so is a linker script, presumably because GNU compulsively overcomplicates everything.

          …I guess that’s enough for the kernel. It might be a linux-only thing, maybe even unintended and well linux doesn’t break userspace.

          Speaking of, I was playing it a bit fast and loose: _start is merely the default symbol name for the entry label, I’m sure nasm and/or ld have ways to set it to something different.

          • JATth@lemmy.world
            link
            fedilink
            English
            arrow-up
            1
            ·
            edit-2
            6 days ago

            Btw, ld.so is a symlink to ld-linux-x86-64.so.2 at least on my system. It is an statically linked executable. The ld.so is, in simpler words, an interpreter for the ELF format and you can run it:

            ld.so --help
            

            Entry point address: 0x1d780

            Which seems to be contained in the only executable section segment of ld.so

            LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000
                 0x0000000000028bb5 0x0000000000028bb5  R E    0x1000
            

            Edit: My understanding of this quite shallow; the above is a segment that in this case contains the entirety of the .text section.

    • MajorasMaskForever@lemmy.world
      link
      fedilink
      English
      arrow-up
      5
      ·
      edit-2
      7 days ago

      You don’t. In C everything gets referenced by a symbol during the link stage of compilation. Libraries ultimately get treated like your source code during compilation and all items land in a symbol table. Two items with the same name result in a link failure and compilation aborts. So a library and a program with main is no bueno.

      When Linux loads an executable they basically look at the program’s symbol table and search for “main” then start executing at that point

      Windows behaves mostly the same way, as does MacOS. Most RTOS’s have their own special way of doing things, bare metal you’re at the mercy of your CPU vendor. The C standard specifies that “main” is the special symbol we all just happen to use

    • anton@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      6 days ago

      If you want to have a library that can also be a standalone executable, just put the main function in an extra file and don’t compile that file when using the library as a library.
      You could also use the preprocessor to do it similar to python but please don’t.

      Just use any build tool, and have two targets, one library and one executable:

      LIB_SOURCES = tools.c, stuff.c, more.c
      EXE_SOURCES = main.c, $LIB_SOURCES
      

      Edit: added example

    • Cratermaker@discuss.tchncs.de
      link
      fedilink
      arrow-up
      2
      ·
      7 days ago

      I haven’t done much low level stuff, but I think the ‘main’ function is something the compiler uses to establish an entry point for the compiled binary. The name ‘main’ would not exist in the compiled binary at all, but the function itself would still exist. Executable formats aren’t all the same, so they’ll have different ways of determining where this entry point function is expected to be. You can ‘run’ a binary library file by invoking a function contained therein, which is how DLL files work.

    • namingthingsiseasy@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      7 days ago

      There are a lot of other helpful replies in this thread, so I won’t add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary:

      • _start calls the libc __libc_start_main;
      • __libc_start_main calls the executable __libc_csu_init (statically-linked part of the libc);
      • __libc_csu_init calls the executable constructors (and other initialisatios);
      • __libc_start_main calls the executable main();
      • __libc_start_main calls the executable exit().
        • grrgyle@slrpnk.net
          link
          fedilink
          arrow-up
          49
          ·
          7 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
            arrow-up
            8
            ·
            7 days ago

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

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

          • Anomalocaris@lemm.ee
            link
            fedilink
            arrow-up
            4
            arrow-down
            1
            ·
            7 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
              3
              ·
              7 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)@lemmy.sdf.org
              link
              fedilink
              arrow-up
              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
              arrow-up
              1
              ·
              7 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. 🤷‍♂️

          • bane_killgrind@slrpnk.net
            link
            fedilink
            English
            arrow-up
            6
            ·
            7 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
              3
              ·
              7 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
                2
                ·
                6 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.

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

            • IronKrill@lemmy.ca
              link
              fedilink
              arrow-up
              12
              ·
              7 days ago

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

              • Lucy :3@feddit.org
                link
                fedilink
                arrow-up
                1
                ·
                6 days ago

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

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

      • brian@programming.dev
        link
        fedilink
        arrow-up
        4
        ·
        7 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
        }
        
      • SpaceNoodle@lemmy.world
        link
        fedilink
        arrow-up
        23
        ·
        7 days ago

        Academic code is absolutely horrific.

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

      • ℍ𝕂-𝟞𝟝@sopuli.xyz
        link
        fedilink
        English
        arrow-up
        16
        ·
        7 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
          15
          ·
          7 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
            11
            ·
            7 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.

    • wise_pancake@lemmy.ca
      link
      fedilink
      arrow-up
      15
      arrow-down
      1
      ·
      7 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
      arrow-up
      10
      ·
      7 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
      arrow-up
      8
      ·
      edit-2
      7 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()
      
    • bastion@feddit.nl
      link
      fedilink
      arrow-up
      7
      ·
      7 days ago

      I always use

      if "__main__" == main:
          __main__()
      

      …and earlier in the code:

      def __main__():
          while True:
              pass
      main = "__main__"
      

      This helps to prevent people from arbitrarily running my code as a library or executable when I don’t went them to.

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

          It simply swaps some things around to make things more confusing, then goes into an infinite loop (whether or not you import or execute it standalone). it’s no different than just including in the global scope:

          while True:
              pass
          

          I was kinda lazy with the fuckery, tbh. I could have gotten much more confusing, but don’t have too much time today. :-)