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.
  • 30p87@feddit.org3 [email protected]

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

    ironkrill@lemmy.caI This user is from outside of this forum
    ironkrill@lemmy.caI This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #77

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

    1 Reply Last reply
    4
    • P [email protected]

      How do you feel about other peoples Go code?

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

      Go code is always an abomination.

      addie@feddit.ukA 1 Reply Last reply
      3
      • M [email protected]

        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

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

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

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

          What's the difference between a "scripting" language and a "real" one?

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

          It's a scripting language. What means that the computer runs it line by line, without needing to get the entire project first.

          F 1 Reply Last reply
          1
          • M [email protected]

            It's a scripting language. What means that the computer runs it line by line, without needing to get the entire project first.

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

            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.

            M F 2 Replies Last reply
            4
            • B [email protected]

              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.

              slacktoid@lemmy.mlS This user is from outside of this forum
              slacktoid@lemmy.mlS This user is from outside of this forum
              [email protected]
              wrote on last edited by
              #82

              Can you elaborate on this blood magic?

              B 1 Reply Last reply
              1
              • M [email protected]

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

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

                procedural programming is more akin to that, but python has far to many oop concepts to be considered procedural imo

                F 1 Reply Last reply
                0
                • F [email protected]

                  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.

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

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

                  F 1 Reply Last reply
                  0
                  • M [email protected]

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

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

                    The -i option is simply interactive mode. All commands still go through a compiler.

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

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

                      Did you know that main is usually a function?

                      1 Reply Last reply
                      7
                      • B [email protected]

                        What is not hacky then in a language design?

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

                        Letting the developer decide what the code should do.

                        B 1 Reply Last reply
                        0
                        • B [email protected]

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

                          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

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

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

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

                            It is normal usage. Though personally I'd probably make another "main" function, to avoid declaring a bunch of global variables

                            N 1 Reply Last reply
                            3
                            • O [email protected]

                              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

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

                              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.

                              J 1 Reply Last reply
                              0
                              • M [email protected]

                                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.

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

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

                                S 1 Reply Last reply
                                3
                                • _ [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?"

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

                                  aren't most of not all conventions hacky anyways?

                                  1 Reply Last reply
                                  0
                                  • A [email protected]

                                    procedural programming is more akin to that, but python has far to many oop concepts to be considered procedural imo

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

                                    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.

                                    A 1 Reply Last reply
                                    1
                                    • arschflugkoerper@feddit.orgA [email protected]

                                      What kind of psychopath would put the code in the if block.

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

                                      you can, no one stopping you

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

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

                                        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?

                                        grrgyle@slrpnk.netG N A 3 Replies Last reply
                                        2
                                        • F [email protected]

                                          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.

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

                                          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

                                          F 1 Reply Last reply
                                          1
                                          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