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

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

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

                Y 1 Reply Last reply
                5
                • A [email protected]

                  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 This user is from outside of this forum
                  F This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #98

                  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.

                  1 Reply Last reply
                  1
                  • J [email protected]

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

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

                    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)

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

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

                      Oh that is a good point actually. It's been a while since I have done any serious Python, so I'm not sure why you couldn't just use convention instead of this conditional.

                      For my part, if a Python script is meant to be executed, then I'll give it a shebang, drop the .py, and simply mark it as executable in the filesystem. 🤷‍♂️

                      1 Reply Last reply
                      0
                      • A [email protected]

                        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?

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

                        This is exactly why the conditional is used. It allows the script to function both as a standalone application and a library.

                        ETA: Probably would make sense to just treat it as default behavior in the interpreter and only require the conditional to overwrite in cases where main is not the main function and/or pre-processing is needed.

                        1 Reply Last reply
                        0
                        • slacktoid@lemmy.mlS [email protected]

                          Can you elaborate on this blood magic?

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

                          It simply swaps some things around to make things more confusing, then goes into an infinite loop (whether or not you import or execute it standalone). it's no different than just including in the global scope:

                          while True:
                              pass
                          

                          I was kinda lazy with the fuckery, tbh. I could have gotten much more confusing, but don't have too much time today. 🙂

                          slacktoid@lemmy.mlS 1 Reply Last reply
                          1
                          • F [email protected]

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

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

                            Yeah. I like using main() that way too. It's usually just a high-level function that handles globals relevant to running in standalone and calling other functions to do work.

                            1 Reply Last reply
                            0
                            • L [email protected]

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

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

                              TIL. Thanks for that!

                              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

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

                                Python: I'm so readable that I'm practically executable pseudo-code

                                Also Python: if __name__ == '__main__': . . .

                                1 Reply Last reply
                                3
                                • A [email protected]

                                  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?

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

                                  The main two reasons that I can think of to include this even when you have no intention of importing this as a library are:

                                  1. For unit testing you will need to import as a module.
                                  2. Sometimes I will run a python interactive interpreter and then import my script so that I can do some manual testing without needing to change my main function or if stmt.
                                  1 Reply Last reply
                                  0
                                  • P [email protected]

                                    How do you feel about other peoples Go code?

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

                                    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.

                                    S S 2 Replies Last reply
                                    4
                                    • S [email protected]

                                      Go code is always an abomination.

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

                                      Succinctly and well put.

                                      1 Reply Last reply
                                      0
                                      • B [email protected]

                                        It simply swaps some things around to make things more confusing, then goes into an infinite loop (whether or not you import or execute it standalone). it's no different than just including in the global scope:

                                        while True:
                                            pass
                                        

                                        I was kinda lazy with the fuckery, tbh. I could have gotten much more confusing, but don't have too much time today. 🙂

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

                                        Lol OK I was wondering how would this run

                                        And yes you should!!

                                        1 Reply Last reply
                                        0
                                        • J [email protected]

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

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

                                          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?

                                          J 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