Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. Python needs an actual default function

Python needs an actual default function

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
159 Posts 89 Posters 1 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O [email protected]

    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

    C This user is from outside of this forum
    C This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #46

    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.

    1 Reply Last reply
    0
    • O [email protected]

      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

      M This user is from outside of this forum
      M This user is from outside of this forum
      [email protected]
      wrote on last edited by [email protected]
      #47

      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

      1 Reply Last reply
      0
      • O [email protected]

        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

        B This user is from outside of this forum
        B This user is from outside of this forum
        [email protected]
        wrote on last edited by [email protected]
        #48

        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.

        O 1 Reply Last reply
        7
        • V [email protected]

          Shouldn't the third panel just be empty?

          buboscandiacus@mander.xyzB This user is from outside of this forum
          buboscandiacus@mander.xyzB This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #49

          It’s fine like this

          1 Reply Last reply
          1
          • hiddenlayer555@lemmy.mlH [email protected]

            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?

            D This user is from outside of this forum
            D This user is from outside of this forum
            [email protected]
            wrote on last edited by [email protected]
            #50

            Still better than having to create a new class just to implement

            public static void main(String[] args) {}
            

            Relevant Fireship video: https://youtu.be/m4-HM_sCvtQ

            D 1 Reply Last reply
            10
            • O [email protected]

              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

              A This user is from outside of this forum
              A This user is from outside of this forum
              [email protected]
              wrote on last edited by [email protected]
              #51

              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

              1 Reply Last reply
              0
              • B [email protected]

                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.netG This user is from outside of this forum
                grrgyle@slrpnk.netG This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #52

                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...

                B 1 Reply Last reply
                1
                • hiddenlayer555@lemmy.mlH [email protected]

                  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?

                  L This user is from outside of this forum
                  L This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #53

                  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

                  W atx_aquarian@lemmy.worldA 2 Replies Last reply
                  11
                  • firelizzard@programming.devF [email protected]

                    What is the point of this?

                    30p87@feddit.org3 This user is from outside of this forum
                    30p87@feddit.org3 This user is from outside of this forum
                    [email protected]
                    wrote on last edited by
                    #54

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

                    ironkrill@lemmy.caI firelizzard@programming.devF 2 Replies Last reply
                    2
                    • L [email protected]

                      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

                      W This user is from outside of this forum
                      W This user is from outside of this forum
                      [email protected]
                      wrote on last edited by
                      #55

                      Php says hello

                      1 Reply Last reply
                      1
                      • D [email protected]

                        Still better than having to create a new class just to implement

                        public static void main(String[] args) {}
                        

                        Relevant Fireship video: https://youtu.be/m4-HM_sCvtQ

                        D This user is from outside of this forum
                        D This user is from outside of this forum
                        [email protected]
                        wrote on last edited by
                        #56

                        Since Java 21, this has been shortened significantly. https://www.baeldung.com/java-21-unnamed-class-instance-main

                        D F M 3 Replies Last reply
                        7
                        • hiddenlayer555@lemmy.mlH [email protected]

                          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?

                          M This user is from outside of this forum
                          M This user is from outside of this forum
                          [email protected]
                          wrote on last edited by [email protected]
                          #57

                          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.

                          T F A 3 Replies Last reply
                          17
                          • D [email protected]

                            Since Java 21, this has been shortened significantly. https://www.baeldung.com/java-21-unnamed-class-instance-main

                            D This user is from outside of this forum
                            D This user is from outside of this forum
                            [email protected]
                            wrote on last edited by
                            #58

                            Free standing functions in Java?! This can't be true.

                            D 1 Reply Last reply
                            2
                            • D [email protected]

                              Free standing functions in Java?! This can't be true.

                              D This user is from outside of this forum
                              D This user is from outside of this forum
                              [email protected]
                              wrote on last edited by
                              #59

                              I know right? It even has var with implicit typing now. While I prefer Kotlin any day, there's been quite a few qol improvements to Java over the last few years.

                              1 Reply Last reply
                              3
                              • tetragrade@leminal.spaceT [email protected]

                                Diabolical

                                B This user is from outside of this forum
                                B This user is from outside of this forum
                                [email protected]
                                wrote on last edited by
                                #60

                                isn't that just normal usage? ..or, did I just whoosh and you were sarcastically saying that?

                                F 1 Reply Last reply
                                6
                                • D [email protected]

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

                                  B This user is from outside of this forum
                                  B This user is from outside of this forum
                                  [email protected]
                                  wrote on last edited by
                                  #61

                                  What is not hacky then in a language design?

                                  _ 1 Reply Last reply
                                  1
                                  • D [email protected]

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

                                    B This user is from outside of this forum
                                    B This user is from outside of this forum
                                    [email protected]
                                    wrote on last edited by
                                    #62

                                    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.

                                    1 Reply Last reply
                                    2
                                    • static_rocket@lemmy.worldS [email protected]

                                      Just cross your fingers nobody attempts to import it...

                                      B This user is from outside of this forum
                                      B This user is from outside of this forum
                                      [email protected]
                                      wrote on last edited by
                                      #63

                                      Due to the oneness of all things, I refuse to distinguish between library code and executable code. One and Zero are arbitrary limitations.

                                      1 Reply Last reply
                                      0
                                      • sheridan@lemmy.worldS [email protected]

                                        Could someone explain this please? I'm still a noob.

                                        B This user is from outside of this forum
                                        B This user is from outside of this forum
                                        [email protected]
                                        wrote on last edited by [email protected]
                                        #64

                                        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.

                                        _ 1 Reply Last reply
                                        3
                                        • hiddenlayer555@lemmy.mlH [email protected]

                                          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. 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 imports it, the value of __name__ will be the name of that file, so main() isn't 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.

                                          D This user is from outside of this forum
                                          D This user is from outside of this forum
                                          [email protected]
                                          wrote on last edited by
                                          #65

                                          Really helpful explanation, thanks.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups