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?

  • @[email protected]
    link
    fedilink
    33
    edit-2
    21 days ago

    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.

      • @[email protected]
        link
        fedilink
        English
        320 days ago

        A scripting language controls an existing binary. A non-scripting language is used to create a new binary.

      • JackbyDev
        link
        fedilink
        English
        220 days ago

        Scripting languages are real. Generally people consider dynamic languages scripting languages but it’s not that simple.

      • @[email protected]
        link
        fedilink
        420 days ago

        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

        • @[email protected]
          link
          fedilink
          1020 days ago

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

          • @[email protected]
            link
            fedilink
            320 days ago

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

            • @[email protected]
              link
              fedilink
              4
              edit-2
              20 days ago

              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.

              • @[email protected]
                link
                fedilink
                220 days ago

                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

                • @[email protected]
                  link
                  fedilink
                  220 days ago

                  I’d say the term “procedural” itself is an issue. Pretty much any language can be done that way if you choose. IIRC, the creator of Clojure wanted Java to work more that way, and he did it by having a single class full of functions. It’s not a natural way to write Java, and that’s why he invented Clojure.

      • @[email protected]
        link
        fedilink
        020 days ago

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

        • @[email protected]
          link
          fedilink
          1320 days ago

          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.

          • @[email protected]
            link
            fedilink
            2
            edit-2
            20 days ago

            Unix shell scripts are one of the few holdouts.

            I don’t know if this applies to other shells, but bash will not only execute your script line-by-line, it will also read it line-by-line. Which means that you can modify the behavior of a running script by editing lines that have not yet been executed*. It’s absolutely bonkers, and I’m sure that it has caused more than one system failure, during upgrades.

            * For example, if you run the following script

            echo "hello"
            sleep 5
            echo "goodbye"
            

            and then edit the third line before the 5 second sleep has elapsed, then the modified line will be executed.

            • JackbyDev
              link
              fedilink
              English
              120 days ago

              I have run into the problem of modifying a bash script while it is running.

    • @[email protected]
      link
      fedilink
      620 days ago

      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…

      • @[email protected]
        link
        fedilink
        4
        edit-2
        20 days ago

        Readability? Me eyes bleed from a day of partially staring at python code, and there is a whole another week of that ahead. Tzinch (Edit: Tzeentch) help me

        • @[email protected]
          link
          fedilink
          420 days ago

          Like in every programming language, it depends who wrote the code. OK, *nearly every programming language, see: LISP.

          You can write cryptic, write-only programs in about any language, but you can even write readable and maintainable PERL scripts (despite people claiming this to be impossible).

          • @[email protected]
            link
            fedilink
            220 days ago

            As much as I am inclined to agree with this, still can’t

            see: LISP

            Also, see: Python with more than three lines of logic. I could suspect that’s just the me-versus-whitespaces thing, but no, YAML files do not get me dizzy in under thirty seconds of reading. Van Rossum made a huge miscalculation here

            • @[email protected]
              link
              fedilink
              English
              420 days ago

              Everyone’s welcome to their opinion of course, but I find Python more readable than anything else and I resent the visual clutter required to make intentions plain in other languages. Feels like having a conversation where people say the words “comma”, “period”, etc.

              I also spend more time with Python than anything else and I suspect these two facts about me relate, lol

              • @[email protected]
                link
                fedilink
                2
                edit-2
                20 days ago

                Someone should get their hands on someone like me and someone like you and study their brains. I spend most time with PHP and C++, and Python looks like an attempt to write code like prose literature. Very interesting how much of this is habbit, as it can’t be just that: reading prose and poetry in English/Russian/Japanese never produced this kind of resentment

                • @[email protected]
                  link
                  fedilink
                  English
                  1
                  edit-2
                  18 days ago

                  I would love that! I do think there are probably interesting underlying personality factors / preferences for a lot of this stuff as well.

                  I do think that many of Python’s characteristics map to my own personality and I bet there’s something to that. Things like syntax of course, but not strictly syntax, also things like “The Zen of Python”, and the way its a “jack-of-all-trades, master-of-none”. I also really kind of need the freedom and accompanying responsibility to break any “rules” on a whim (Python will happily let you overwrite its own internals while running, for instance), but I almost never do anything that uses it…

                  I could probably keep going lol. Feels like a “people looking like their pets” scenario, lmao

    • @[email protected]OP
      link
      fedilink
      English
      3
      edit-2
      19 days ago

      Luckily Python is one step ahead:

      Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> if __name__ = "__main__":
      ... 
      ...    main()
      ...    
          File "<python-input-0>", line 1
          if __name__ = "__main__":
              ^^^^^^^^^^^^^^^^^^^^^
      SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
      

      Also TIL that := is a thing in Python.

      • @[email protected]
        link
        fedilink
        1
        edit-2
        19 days ago

        yea I also couldnt get the formatting to work right, triple quotes kept turning things into accented letters, so I gave up.

        and also := also known as the walrus operator is very fun and sometimes very convenient to use

  • @[email protected]
    link
    fedilink
    821 days ago

    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.

  • Eager Eagle
    link
    fedilink
    English
    2821 days ago

    The if block is still in the global scope, so writing the code in it is a great way to find yourself scratching your head with a weird bug 30 minutes later.

  • @[email protected]
    link
    fedilink
    English
    2221 days 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?”

    • Dr. Moose
      link
      fedilink
      English
      22
      edit-2
      21 days ago

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

          • @[email protected]
            link
            fedilink
            120 days ago

            I don’t understand. What do you mean by deciding what the code should do in the context of language design? Can you give a concrete example? I am confused because the “main” function is required when you make an executable. Otherwise, a library will not contain any main function and we could compile it just fine no? (Shared library)

            • @[email protected]
              link
              fedilink
              220 days ago

              Python is an interpreted language that doesn’t need a main function explicitly. You can define any package entry points you want at the package config level. (setup.py, etc)

              example: What I meant was I prefer language that treat developers like adults. If I want ptrhon’s “ux” to hide some functions or objects I can do that with underscores, but nothing is private, a developer using my library can do whatever they want with it, access whatever internals they want (at their own risk of course)

        • Dr. Moose
          link
          fedilink
          English
          119 days ago

          Most contemporary python tools like flask or uvicorn do exactly this and require an explicit entry point

      • @[email protected]
        link
        fedilink
        1020 days ago

        Yeah, this is it.

        What’s hacky about an introspective language providing environment to all of the executing code, so that the coder can make the decision about what to do?

        It would by hacky if Python decided “We’ll arbitrarily take functions named “main” and execute them for you, even though we already started execution at the top of the file.”

        For C, this is less so. The body of the file isn’t being executed, it’s being read and compiled. Without a function to act as a starting point, it doesn’t get executed.

      • @[email protected]
        link
        fedilink
        820 days ago

        Reserving main is definitely more hacky. Try compiling multiple objects with main defined into a single binary - it won’t go well. This can make a lot of testing libraries rather convoluted, since some want to write their own main while others want you to write it because require all kinds of macros or whatever.

        On the other hand, if __name__ == "__main__" very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.

    • @[email protected]
      link
      fedilink
      320 days ago

      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.

      • @[email protected]
        link
        fedilink
        120 days ago

        Due to the oneness of all things, I refuse to distinguish between library code and executable code. One and Zero are arbitrary limitations.

    • @[email protected]
      link
      fedilink
      1421 days ago

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

      • @[email protected]
        link
        fedilink
        220 days ago

        Dumb person question: if it’s good practice to do this so things don’t go sideways, shouldn’t it be a built-in feature/utility/function/whatever?

        • @[email protected]
          link
          fedilink
          2
          edit-2
          20 days ago

          It is “built-in” as the name is part of python. However, Python runs top to bottom, rather than having a special entrypoint. So name is just a utility you can use in your design.

          While it can be a good practice to define a main entrypoint, that’s more of a design decision and not hard rule. Many applications would not benefit from it because there is only one way to actually call the application to begin with.

          Edit: Also not a dumb question. All programming languages have unique elements to them due to how they were envisioned and or developed over time (Pythons 30 years old)

  • @[email protected]
    link
    fedilink
    English
    6
    edit-2
    21 days ago

    Alternative: put entry point code in file __main__.py & run the containing package (eg, some_package) as a top-level expression (eg, python -m some_package).

  • @[email protected]
    link
    fedilink
    3921 days ago

    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

    • @[email protected]
      link
      fedilink
      3621 days 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.

        • @[email protected]
          link
          fedilink
          320 days ago

          I used it for a while and I think it’s been one of the best languages I’ve tried. C for example is too barebones for modern desktop apps. Apps written in Rust are great but most of the time, it’s just not worth the effort. And stuff like Python, JS is… uhh… where do I even begin

          I think Go hits the sweet spot between these. Unlike C, it at least has some simple error/panic mechanism, GC so you don’t have to worry about memory much and some modern features on top of that. And unlike Python it can actually create reasonably snappy programs.

          In any programming language, there will always be multiple cases where you need to link C libraries. CGo, although people don’t seem to be adoring it, is actually… okay? I mean of course it does still have some overhead but it’s still one of the nicer ways to link C libraries with your code. And Go being similar to C makes writing bindings so much easier

          Multithreading in Go is lovely. Or as I read somewhere “you merely adopted multithreading, I was born with it”

          Packaging is handled pretty nicely, pulling a library from the net is fairly trivial. And the standard directory structure for Go, although I’m not used to it, makes organizing stuff much easier and is easy to adopt

          As you would’ve guessed from the amount of times I mentioned C in this comment, I basically see Go as the “bigger C for different situations”

        • @[email protected]
          link
          fedilink
          820 days 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.

          • @[email protected]
            link
            fedilink
            320 days ago

            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

          • @[email protected]
            link
            fedilink
            English
            219 days 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.

      • @[email protected]
        link
        fedilink
        219 days 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.

        • @[email protected]
          link
          fedilink
          219 days 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.

      • @[email protected]
        link
        fedilink
        820 days ago

        Agreed. I program mainly in C so its easier for me to make sense of bad C code than bad python code which just makes me cry

        • jadedwench [they/them]
          link
          fedilink
          English
          120 days ago

          Does Lua rank far below python for you? I have so much rage against it. At least with Python I don’t have to do a bunch of steps to just get it to do something. May take me a while to get through bad Python code, but Lua makes my eyes bleed and I begin to regret my life choices in trying to understand wtf these people wrote.

          • @[email protected]
            link
            fedilink
            119 days ago

            Never had the opportunity to work with it. Python is much more pervasive compared to Lua I reckon

  • Die Martin Die
    link
    fedilink
    8
    edit-2
    21 days ago
    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.

  • @[email protected]
    link
    fedilink
    620 days ago

    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

    • @[email protected]
      link
      fedilink
      319 days ago

      All of mine are called do_thing() because after a few days of working on it, the scope creep always means the original name was wrong anyway.