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.
  • sinthesis@lemmy.todayS [email protected]

    I use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params event and context. So I would do something like this:

    def handler(event, context):
        things
        return {
            'statusCode': 200,
            'body': 'Hello from Lambda!'
        }
    
    if __name__ == '__main__':
        event = {}
        context = {}
        response = handler(event, context)
        print(response)
    
    tetragrade@leminal.spaceT This user is from outside of this forum
    tetragrade@leminal.spaceT This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #44

    Diabolical

    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?

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

      Shouldn't the third panel just be empty?

      buboscandiacus@mander.xyzB 1 Reply Last reply
      1
      • 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
                                          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