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