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.
  • 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]
    #31
    if debug.getinfo(1).what == "main" then
      -- ...
    end
    

    Not that you'll ever use it. No, seriously.

    Edit: actually, they are not quite equivalent. This code just checks whether we are outside any function, not necessarily in the main file (i.e. not in a module). I don't think there's an equivalent to Python's __name__ in stock Lua.

    1 Reply Last reply
    2
    • Q [email protected]

      Nothing prevents you from putting a call to “main()” in the global scope

      static_rocket@lemmy.worldS This user is from outside of this forum
      static_rocket@lemmy.worldS This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #32

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

      B 1 Reply Last reply
      2
      • Q [email protected]

        Nothing prevents you from putting a call to “main()” in the global scope

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

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

        L 1 Reply Last reply
        7
        • grrgyle@slrpnk.netG [email protected]

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

          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
          #34

          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 1 Reply Last reply
          2
          • 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?

            embed_me@programming.devE This user is from outside of this forum
            embed_me@programming.devE This user is from outside of this forum
            [email protected]
            wrote on last edited by
            #35

            Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language

            S addie@feddit.ukA 2 Replies Last reply
            17
            • 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?

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

              Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: "Yeah, that is a bit hacky, isn't it?"

              D A N 3 Replies Last reply
              6
              • embed_me@programming.devE [email protected]

                Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language

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

                Sounds like a skill issue on your end

                embed_me@programming.devE 1 Reply Last reply
                9
                • 30p87@feddit.org3 [email protected]

                  Never heard of

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

                  ?

                  firelizzard@programming.devF This user is from outside of this forum
                  firelizzard@programming.devF This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #38

                  What is the point of this?

                  30p87@feddit.org3 1 Reply Last reply
                  0
                  • 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?

                    sinthesis@lemmy.todayS This user is from outside of this forum
                    sinthesis@lemmy.todayS This user is from outside of this forum
                    [email protected]
                    wrote on last edited by [email protected]
                    #39

                    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 1 Reply Last reply
                    3
                    • embed_me@programming.devE [email protected]

                      Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language

                      addie@feddit.ukA This user is from outside of this forum
                      addie@feddit.ukA This user is from outside of this forum
                      [email protected]
                      wrote on last edited by
                      #40

                      I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.

                      To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.

                      Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.

                      P driving_crooner@lemmy.eco.brD E 3 Replies Last reply
                      13
                      • _ [email protected]

                        Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: "Yeah, that is a bit hacky, isn't it?"

                        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]
                        #41

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

                        B B N 3 Replies Last reply
                        15
                        • addie@feddit.ukA [email protected]

                          I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.

                          To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.

                          Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.

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

                          How do you feel about other peoples Go code?

                          S addie@feddit.ukA yogurtwrong@lemmy.worldY 3 Replies Last reply
                          0
                          • 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?

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

                            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 M B A N 5 Replies Last reply
                            1
                            • 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
                                          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