Also, do y’all call main() in the if block or do you just put the code you want to run in the if block?

  • embed_me@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    11 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

    • addie@feddit.uk
      link
      fedilink
      arrow-up
      0
      ·
      11 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.

      • ebc@lemmy.ca
        link
        fedilink
        arrow-up
        0
        ·
        10 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.

        • addie@feddit.uk
          link
          fedilink
          arrow-up
          1
          ·
          10 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.

  • JATth@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    10 days ago

    I would put my code in a def main(), so that the local names don’t escape into the module scope:

    if __name__ == '__main__':
        def main():
            print('/s')
        main()
    

    (I didn’t see this one yet here.)

    • YourShadowDani@lemm.ee
      link
      fedilink
      English
      arrow-up
      0
      ·
      10 days ago

      I’m a little new to Python standards. Is this better or worse than putting the def main(): outside the if statement (but calling main() inside it)

      • JATth@lemmy.world
        link
        fedilink
        English
        arrow-up
        0
        ·
        10 days ago

        I intended this an sarcastic example; I think it’s worse than putting the main outside of the branch because of the extra indent-level. It does have an upside that the main() doesn’t exist if you try import this as an module.

  • _____@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    11 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@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      11 days ago

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

      • bastion@feddit.nl
        link
        fedilink
        arrow-up
        1
        ·
        11 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.

  • Eager Eagle@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    11 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.