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.
  • eager_eagle@lemmy.worldE [email protected]

    the best thing is when not even the author knows the correct order of running the cells; because of course it isn't top-to-bottom.

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

    Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.

    And don't forget the number one rule: you must use all the graphing libraries, all the time.

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

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

      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 [email protected]
      #27

      If the file is just a class I usually put example usage with some default arguments in that block by itself. There is no reason for a "main" function. It's a nice obvious block that doesn't run when someone imports the class but if they're looking at the class there is a really obvious place to see the class usage. No confusion about what "main()" is meant to do.

      if __name__ == '__main__':
          # MyClass example Usage
          my_object = MyClass()
          my_object.my_method()
      
      1 Reply Last reply
      1
      • S [email protected]

        Academic code is absolutely horrific.

        Fortunately, it is possible to translate it for practical applications.

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

        As someone in academia who writes code, I can confirm.

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

          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 [email protected]
          #29

          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 1 Reply Last reply
          3
          • arschflugkoerper@feddit.orgA [email protected]

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

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

            Why would you waste a function call on something so completely redundant?

            ~For real though, arg parsing goes in the if, then gets dispatched to whatever function call is needed to run the proper script.~

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

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

              1 Reply Last reply
              2
              • Q [email protected]

                Nothing prevents you from putting a call to “main()” in the global scope

                static_rocket@lemmy.worldS This user is from outside of this forum
                static_rocket@lemmy.worldS This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #32

                Just cross your fingers nobody attempts to import it...

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

                  Nothing prevents you from putting a call to “main()” in the global scope

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

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

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

                    Now think about this, you have logic that doesn't make sense when run directly, but you need it to be a library.

                    You have multiple name=main statements in some of your functions

                    grrgyle@slrpnk.netG 1 Reply Last reply
                    2
                    • 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?

                      embed_me@programming.devE This user is from outside of this forum
                      embed_me@programming.devE This user is from outside of this forum
                      [email protected]
                      wrote on last edited by
                      #35

                      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

                      S addie@feddit.ukA 2 Replies Last reply
                      17
                      • 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?

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

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

                        D A N 3 Replies Last reply
                        6
                        • embed_me@programming.devE [email protected]

                          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

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

                          Sounds like a skill issue on your end

                          embed_me@programming.devE 1 Reply Last reply
                          9
                          • 30p87@feddit.org3 [email protected]

                            Never heard of

                            def main():
                                pass
                            
                            if __name__ == '__main__':
                                main()
                            

                            ?

                            firelizzard@programming.devF This user is from outside of this forum
                            firelizzard@programming.devF This user is from outside of this forum
                            [email protected]
                            wrote on last edited by
                            #38

                            What is the point of this?

                            30p87@feddit.org3 1 Reply Last reply
                            0
                            • 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?

                              sinthesis@lemmy.todayS This user is from outside of this forum
                              sinthesis@lemmy.todayS This user is from outside of this forum
                              [email protected]
                              wrote on last edited by [email protected]
                              #39

                              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)
                              
                              tetragrade@leminal.spaceT 1 Reply Last reply
                              3
                              • embed_me@programming.devE [email protected]

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

                                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.

                                P driving_crooner@lemmy.eco.brD E 3 Replies Last reply
                                13
                                • _ [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?"

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

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

                                  B B N 3 Replies Last reply
                                  15
                                  • addie@feddit.ukA [email protected]

                                    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.

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

                                    How do you feel about other peoples Go code?

                                    S addie@feddit.ukA yogurtwrong@lemmy.worldY 3 Replies Last reply
                                    0
                                    • 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?

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

                                      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

                                      C M B A N 5 Replies Last reply
                                      1
                                      • sinthesis@lemmy.todayS [email protected]

                                        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)
                                        
                                        tetragrade@leminal.spaceT This user is from outside of this forum
                                        tetragrade@leminal.spaceT This user is from outside of this forum
                                        [email protected]
                                        wrote on last edited by
                                        #44

                                        Diabolical

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

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

                                          Shouldn't the third panel just be empty?

                                          buboscandiacus@mander.xyzB 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