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?

    • addie ( addie@feddit.uk ) 
      link
      fedilink
      arrow-up
      26
      ·
      1 year ago

      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.

        • addie ( addie@feddit.uk ) 
          link
          fedilink
          arrow-up
          7
          ·
          1 year ago

          Well now. My primary exposure to Go would be using it to take first place in my company’s ‘Advent of Code’ several years ago, in order to see what it was like, after which I’ve been pleased never to have to use it again. Some of our teams have used it to provide microservices - REST APIs that do database queries, some lightweight logic, and conversion to and from JSON - and my experience of working with that is that they’ve inexplicably managed to scatter all the logic among dozens of files, for what might be done with 80 lines of Python. I suspect the problem in that case is the developers, though.

          It has some good aspects - I like how easy it is to do a static build that can be deployed in a container.

          The actual language itself I find fairly abominable. The lack of exceptions means that error handling is all through everything, and not necessarily any better than other modern languages. The lack of overloads means that you’ll have multiple definitions of eg. Math.min cluttering things up. I don’t think the container classes are particularly good. The implementation of pointers seems solely implemented to let you have null pointer exceptions, it’s a pointless wart.

          If what you’re wanting to code is the kind of thing that Google do, in the exact same way that Google do it, and you have a team of hipsters who all know how it works, then it may be a fine choice. Otherwise I would probably recommend using something else.

          • This is the most excellent summary of Go I have ever read. I agree with everything you’ve said, although as a fan of Scala and in particular its asynchronous programming ecosystem (cats for me, but I’ll forgive those who prefer the walled garden of zio) I would also add that, whilst its async model with go routines is generally pretty easy to use, it can shit the bed on some highly-concurrent workloads and fail to schedule stuff in time that it really should’ve, and because it’s such a mother-knows-best language there’s fuck all you can do to give higher priority to the threads that you happen to know need more TLC

          • sping ( sping@lemmy.sdf.org ) 
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 year ago

            I’m now 1 year in to working in Go having been mostly C++ and then mostly large-scale Python dev (with full type annotation).

            Frankly, I bristle now at people giving Python a hard time, having worked with Go and I now hate Go and the de-facto ethos that surrounds it. Python may be slow, but for a lot of use cases not in any way that matters and modern computers are very fast. Many problem areas are not performance-limited, and many performance problems are algorithmic, not from raw statement execution. I even rewrote an entire system in Python and made it use 20% of the CPU the former C++ solution used, while having much more functionality.

            The error returns drive me nuts. I looked around for explanations of the reasoning as I wasn’t seeing it, and only found bald assertions that exceptions get out of control and somehow error returns don’t. Meanwhile standard Go code is very awkward to read because almost every little trivial function calls becomes 4 lines of code, often to do nothing but propagate the error (and errors are just ignored if you forget…). With heavy use of context managers, my error and cancellation handling in Python was always clean, clear, and simple, with code that almost read like whiteboard pseudo-code.

            The select statement can be cool in Go, but then you realize that literally 98% of the times it’s used, it’s simply boilerplate code to (verbosely) handle cancellation semantics via the context object you have to pass everywhere. Again, literally code you just don’t need in exception-based languages with good structures to manage it like Python context managers.

            And every time you think “this is stupidly awkward and verbose, surely there’s a cleaner way to do this” you find people online advocating writing the same boilerplate code and passing it off as a virtue. e.g. get a value from a map and fall back to a default if it’s not there? Nope, not offering that, so everyone must write their own if foo, ok := m[k]; !ok {...} crap. Over and over and over again the answer is “just copy this chunk of code” rather than “standard libraries should provide these commonly needed utilities”. Of course we can do anything we want ourselves, it’s Turing Complete, but why would we want to perpetually reinvent these wheels?

            It’s an unpopular language, becoming less popular (at least by Google trends) and for good reason. I can see it working well for a narrow set of low level activities with extreme concurrency performance needs, but it’s not the only language that could handle that, and for everything else, I think it’s the wrong choice.

      • ebc ( ebc@lemmy.ca ) 
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        other people’s Java

        I’m gonna have to disagree here, it’s always a guessing game of how many layers of abstraction they’ve used to seemingly avoid writing any implementation code… Can’t put the code related to “bicycles” in the Bicycle class, no, that obviously goes in WheeledDeviceServiceFactoryBeanImpl that’s in the ‘utils’ package.

        • addie ( addie@feddit.uk ) 
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          Enough of that crazy talk - plainly WheeledDeviceServiceFactoryBeanImpl is where the dependency injection annotations are placed. If you can decide what the code does without stepping through it with a debugger, and any backtrace doesn’t have at least two hundred lines of Spring boot, then plainly it isn’t enterprise enough.

          Fair enough, though. You can write stupid overly-abstract shit in any language, but Java does encourage it.

  • Laurel Raven ( laurelraven@lemmy.zip ) 
    link
    fedilink
    English
    arrow-up
    27
    ·
    1 year ago

    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

  • _____ ( _____@lemm.ee ) 
    link
    fedilink
    English
    arrow-up
    19
    ·
    1 year ago

    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?”

    • Is it? I really don’t think so. What can you propose that’s better? I think if __name__ == __main__ works perfectly fine and can’t really think of anything that would be better.

      And you don’t have to use it either if you don’t want to anyway, so no, I don’t think it’s that much of a hack. Especially when the comic compares C as an example, which makes no sense to me whatsoever.

  • Sinthesis ( Sinthesis@lemmy.today ) 
    link
    fedilink
    English
    arrow-up
    12
    ·
    edit-2
    1 year ago

    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)
    
  • Call the function from the if block.

    Now your tests can more easily call it.

    I think at my last job we did argument parsing in the if block, and passed stuff into the main function.

  • Does everyone call the function of the script main? I never use main(), just call the function what the program is supposed to do, this program calculates the IBNR? The function is called calculate_IBNR(), then at the end of the script if name = ‘main’: calculate_IBNR(test_params) to test de script, then is imported into a tkinter script to be converter to an exe with pyinstaller

  • 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

    • barsoap ( barsoap@lemm.ee ) 
      link
      fedilink
      arrow-up
      16
      ·
      edit-2
      1 year ago

      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.

      • 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

        • barsoap ( barsoap@lemm.ee ) 
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          1 year ago

          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.

    • anton ( anton@lemmy.blahaj.zone ) 
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      1 year ago

      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

    • There are a lot of other helpful replies in this thread, so I won’t add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary:

      • _start calls the libc __libc_start_main;
      • __libc_start_main calls the executable __libc_csu_init (statically-linked part of the libc);
      • __libc_csu_init calls the executable constructors (and other initialisatios);
      • __libc_start_main calls the executable main();
      • __libc_start_main calls the executable exit().
    • 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.

  • 10001110101 ( 10001110101@lemm.ee ) 
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    I’ve always found needing to manually add a class instance parameter (i.e. self) to every object method really weird. And the constructors being named __init__. Not having multiple dispatch is kinda annoying too. Needing to use decorators for class methods, static methods, and abstract classes is also annoying. Now that I think about it, Python kinda sucks (even though it’s the language I use the most, lol).

      • 10001110101 ( 10001110101@lemm.ee ) 
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 year ago

        Guess I just prefer languages that do it this way:

        class AClass {
          var aProp = 0
        
          fun aMethod() {
            aProp++
          }
        }
        

        Though I suppose confusion and bugs can happen when you do something like:

        class AClass {
          var aProp = 0
        
          fun aMethod(aProp: Int) {
            // `this.aProp` is needed to access the property
          }
        }