▲ 733 ▼ Python needs an actual default function (lemmy.ml) submitted 1 year ago by HiddenLayer555@lemmy.ml to c/programmer_humor@programming.dev 160 comments fedilink hide all child comments 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?
[–] arschflugkoerper@feddit.org 88 points 1 year ago (6 children) What kind of psychopath would put the code in the if block. permalink fedilink source hideshow 12 child comments replies: [–] HiddenLayer555@lemmy.ml [S] 73 points 1 year ago* (2 children) Looks at all the Python scripts in my bin folder that I wrote. permalink fedilink source parent hideshow 4 child comments replies: [–] 30p87@feddit.org 64 points 1 year ago* (3 children) Never heard of def main(): pass if __name__ == '__main__': main() ? permalink fedilink source parent hideshow 6 child comments replies: [–] grrgyle@slrpnk.net 49 points 1 year ago (3 children) I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af permalink fedilink source parent hideshow 6 child comments replies: [–] frezik@midwest.social 9 points 1 year ago Python: I'm so readable that I'm practically executable pseudo-code Also Python: if __name__ == '__main__': . . . permalink fedilink source parent [–] bane_killgrind@slrpnk.net 7 points 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] grrgyle@slrpnk.net 3 points 1 year ago (1 child) I'm not sure I'm following the implication. Name=main is for scripts primary, is it not? I've never thought to add more than one of these conditionals anyway... permalink fedilink source parent hideshow 2 child comments replies: [–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent [–] Anomalocaris@lemm.ee 3 points 1 year ago (3 children) 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? permalink fedilink source parent hideshow 6 child comments replies: [–] Archr@lemmy.world 3 points 1 year ago 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: For unit testing you will need to import as a module. 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. permalink fedilink source parent [–] nickwitha_k@lemmy.sdf.org 1 point 1 year ago* 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. permalink fedilink source parent [–] grrgyle@slrpnk.net 1 point 1 year ago 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. 🤷♂️ permalink fedilink source parent [–] HiddenLayer555@lemmy.ml [S] 24 points 1 year ago* Heard of it, was too lazy to do it that way. To be fair I now do it that way, but not when I was learning Python. permalink fedilink source parent [–] firelizzard@programming.dev 4 points 1 year ago (1 child) What is the point of this? permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 7 points 1 year ago (2 children) Not having tons of code in one if statement, but in a function. permalink fedilink source parent hideshow 4 child comments replies: [–] IronKrill@lemmy.ca 12 points 1 year ago And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function. permalink fedilink source parent [–] firelizzard@programming.dev 2 points 1 year ago (1 child) I thought you were saying to literally use def main(): pass, that’s why I was confused permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent [–] Anomalocaris@lemm.ee 11 points 1 year ago (1 child) custom bin folders are a realm no God dares to tread permalink fedilink source parent hideshow 2 child comments replies: [–] SubArcticTundra@lemmy.ml 2 points 1 year ago* I can and I do *trollface* permalink fedilink source parent [–] eager_eagle@lemmy.world 33 points 1 year ago* (3 children) I work in an academic / research environment. Depending who wrote it, even seeing a __name__ == "__main__" is a bit of a rare thing... permalink fedilink source parent hideshow 6 child comments replies: [–] SpaceNoodle@lemmy.world 23 points 1 year ago (1 child) Academic code is absolutely horrific. Fortunately, it is possible to translate it for practical applications. permalink fedilink source parent hideshow 2 child comments replies: [–] arbitrary_sarcasm@lemmy.world 13 points 1 year ago As someone in academia who writes code, I can confirm. permalink fedilink source parent [–] HK65@sopuli.xyz 16 points 1 year ago (1 child) Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope? permalink fedilink source parent hideshow 2 child comments replies: [–] eager_eagle@lemmy.world 15 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] HK65@sopuli.xyz 11 points 1 year ago 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. permalink fedilink source parent [–] brian@programming.dev 4 points 1 year ago python isn't the only language to do "execute everything imported from a particular file and all top level statements get run". both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I'm sure there's more. python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn't hurt either. for instance in node this is the equivalent, even though I've never seen someone try before: if (path.resolve(url.fileURLToPath(import.meta.url)).includes(path.resolve(process.argv[1]))) { // main things } permalink fedilink source parent [–] wise_pancake@lemmy.ca 15 points 1 year ago 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. permalink fedilink source parent [–] NeatNit@discuss.tchncs.de 10 points 1 year ago I definitely do for quick scripts, but I try to break this habit. The biggest advantage of def main() is that variables are local and not accessible to other functions defined in the same script, which can sometimes help catch bugs or typos. permalink fedilink source parent [–] LeninOnAPrayer@lemm.ee 8 points 1 year ago* 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() permalink fedilink source parent [–] Anomalocaris@lemm.ee 2 points 1 year ago you can, no one stopping you permalink fedilink source parent
[–] HiddenLayer555@lemmy.ml [S] 73 points 1 year ago* (2 children) Looks at all the Python scripts in my bin folder that I wrote. permalink fedilink source parent hideshow 4 child comments replies: [–] 30p87@feddit.org 64 points 1 year ago* (3 children) Never heard of def main(): pass if __name__ == '__main__': main() ? permalink fedilink source parent hideshow 6 child comments replies: [–] grrgyle@slrpnk.net 49 points 1 year ago (3 children) I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af permalink fedilink source parent hideshow 6 child comments replies: [–] frezik@midwest.social 9 points 1 year ago Python: I'm so readable that I'm practically executable pseudo-code Also Python: if __name__ == '__main__': . . . permalink fedilink source parent [–] bane_killgrind@slrpnk.net 7 points 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] grrgyle@slrpnk.net 3 points 1 year ago (1 child) I'm not sure I'm following the implication. Name=main is for scripts primary, is it not? I've never thought to add more than one of these conditionals anyway... permalink fedilink source parent hideshow 2 child comments replies: [–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent [–] Anomalocaris@lemm.ee 3 points 1 year ago (3 children) 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? permalink fedilink source parent hideshow 6 child comments replies: [–] Archr@lemmy.world 3 points 1 year ago 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: For unit testing you will need to import as a module. 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. permalink fedilink source parent [–] nickwitha_k@lemmy.sdf.org 1 point 1 year ago* 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. permalink fedilink source parent [–] grrgyle@slrpnk.net 1 point 1 year ago 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. 🤷♂️ permalink fedilink source parent [–] HiddenLayer555@lemmy.ml [S] 24 points 1 year ago* Heard of it, was too lazy to do it that way. To be fair I now do it that way, but not when I was learning Python. permalink fedilink source parent [–] firelizzard@programming.dev 4 points 1 year ago (1 child) What is the point of this? permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 7 points 1 year ago (2 children) Not having tons of code in one if statement, but in a function. permalink fedilink source parent hideshow 4 child comments replies: [–] IronKrill@lemmy.ca 12 points 1 year ago And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function. permalink fedilink source parent [–] firelizzard@programming.dev 2 points 1 year ago (1 child) I thought you were saying to literally use def main(): pass, that’s why I was confused permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent [–] Anomalocaris@lemm.ee 11 points 1 year ago (1 child) custom bin folders are a realm no God dares to tread permalink fedilink source parent hideshow 2 child comments replies: [–] SubArcticTundra@lemmy.ml 2 points 1 year ago* I can and I do *trollface* permalink fedilink source parent
[–] 30p87@feddit.org 64 points 1 year ago* (3 children) Never heard of def main(): pass if __name__ == '__main__': main() ? permalink fedilink source parent hideshow 6 child comments replies: [–] grrgyle@slrpnk.net 49 points 1 year ago (3 children) I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af permalink fedilink source parent hideshow 6 child comments replies: [–] frezik@midwest.social 9 points 1 year ago Python: I'm so readable that I'm practically executable pseudo-code Also Python: if __name__ == '__main__': . . . permalink fedilink source parent [–] bane_killgrind@slrpnk.net 7 points 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] grrgyle@slrpnk.net 3 points 1 year ago (1 child) I'm not sure I'm following the implication. Name=main is for scripts primary, is it not? I've never thought to add more than one of these conditionals anyway... permalink fedilink source parent hideshow 2 child comments replies: [–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent [–] Anomalocaris@lemm.ee 3 points 1 year ago (3 children) 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? permalink fedilink source parent hideshow 6 child comments replies: [–] Archr@lemmy.world 3 points 1 year ago 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: For unit testing you will need to import as a module. 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. permalink fedilink source parent [–] nickwitha_k@lemmy.sdf.org 1 point 1 year ago* 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. permalink fedilink source parent [–] grrgyle@slrpnk.net 1 point 1 year ago 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. 🤷♂️ permalink fedilink source parent [–] HiddenLayer555@lemmy.ml [S] 24 points 1 year ago* Heard of it, was too lazy to do it that way. To be fair I now do it that way, but not when I was learning Python. permalink fedilink source parent [–] firelizzard@programming.dev 4 points 1 year ago (1 child) What is the point of this? permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 7 points 1 year ago (2 children) Not having tons of code in one if statement, but in a function. permalink fedilink source parent hideshow 4 child comments replies: [–] IronKrill@lemmy.ca 12 points 1 year ago And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function. permalink fedilink source parent [–] firelizzard@programming.dev 2 points 1 year ago (1 child) I thought you were saying to literally use def main(): pass, that’s why I was confused permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent
[–] grrgyle@slrpnk.net 49 points 1 year ago (3 children) I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af permalink fedilink source parent hideshow 6 child comments replies: [–] frezik@midwest.social 9 points 1 year ago Python: I'm so readable that I'm practically executable pseudo-code Also Python: if __name__ == '__main__': . . . permalink fedilink source parent [–] bane_killgrind@slrpnk.net 7 points 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] grrgyle@slrpnk.net 3 points 1 year ago (1 child) I'm not sure I'm following the implication. Name=main is for scripts primary, is it not? I've never thought to add more than one of these conditionals anyway... permalink fedilink source parent hideshow 2 child comments replies: [–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent [–] Anomalocaris@lemm.ee 3 points 1 year ago (3 children) 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? permalink fedilink source parent hideshow 6 child comments replies: [–] Archr@lemmy.world 3 points 1 year ago 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: For unit testing you will need to import as a module. 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. permalink fedilink source parent [–] nickwitha_k@lemmy.sdf.org 1 point 1 year ago* 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. permalink fedilink source parent [–] grrgyle@slrpnk.net 1 point 1 year ago 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. 🤷♂️ permalink fedilink source parent
[–] frezik@midwest.social 9 points 1 year ago Python: I'm so readable that I'm practically executable pseudo-code Also Python: if __name__ == '__main__': . . . permalink fedilink source parent
[–] bane_killgrind@slrpnk.net 7 points 1 year ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] grrgyle@slrpnk.net 3 points 1 year ago (1 child) I'm not sure I'm following the implication. Name=main is for scripts primary, is it not? I've never thought to add more than one of these conditionals anyway... permalink fedilink source parent hideshow 2 child comments replies: [–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent
[–] grrgyle@slrpnk.net 3 points 1 year ago (1 child) I'm not sure I'm following the implication. Name=main is for scripts primary, is it not? I've never thought to add more than one of these conditionals anyway... permalink fedilink source parent hideshow 2 child comments replies: [–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent
[–] bane_killgrind@slrpnk.net 2 points 1 year ago So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers. So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script. permalink fedilink source parent
[–] Anomalocaris@lemm.ee 3 points 1 year ago (3 children) 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? permalink fedilink source parent hideshow 6 child comments replies: [–] Archr@lemmy.world 3 points 1 year ago 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: For unit testing you will need to import as a module. 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. permalink fedilink source parent [–] nickwitha_k@lemmy.sdf.org 1 point 1 year ago* 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. permalink fedilink source parent [–] grrgyle@slrpnk.net 1 point 1 year ago 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. 🤷♂️ permalink fedilink source parent
[–] Archr@lemmy.world 3 points 1 year ago 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: For unit testing you will need to import as a module. 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. permalink fedilink source parent
[–] nickwitha_k@lemmy.sdf.org 1 point 1 year ago* 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. permalink fedilink source parent
[–] grrgyle@slrpnk.net 1 point 1 year ago 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. 🤷♂️ permalink fedilink source parent
[–] HiddenLayer555@lemmy.ml [S] 24 points 1 year ago* Heard of it, was too lazy to do it that way. To be fair I now do it that way, but not when I was learning Python. permalink fedilink source parent
[–] firelizzard@programming.dev 4 points 1 year ago (1 child) What is the point of this? permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 7 points 1 year ago (2 children) Not having tons of code in one if statement, but in a function. permalink fedilink source parent hideshow 4 child comments replies: [–] IronKrill@lemmy.ca 12 points 1 year ago And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function. permalink fedilink source parent [–] firelizzard@programming.dev 2 points 1 year ago (1 child) I thought you were saying to literally use def main(): pass, that’s why I was confused permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent
[–] 30p87@feddit.org 7 points 1 year ago (2 children) Not having tons of code in one if statement, but in a function. permalink fedilink source parent hideshow 4 child comments replies: [–] IronKrill@lemmy.ca 12 points 1 year ago And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function. permalink fedilink source parent [–] firelizzard@programming.dev 2 points 1 year ago (1 child) I thought you were saying to literally use def main(): pass, that’s why I was confused permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent
[–] IronKrill@lemmy.ca 12 points 1 year ago And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function. permalink fedilink source parent
[–] firelizzard@programming.dev 2 points 1 year ago (1 child) I thought you were saying to literally use def main(): pass, that’s why I was confused permalink fedilink source parent hideshow 2 child comments replies: [–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent
[–] 30p87@feddit.org 1 point 1 year ago Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder permalink fedilink source parent
[–] Anomalocaris@lemm.ee 11 points 1 year ago (1 child) custom bin folders are a realm no God dares to tread permalink fedilink source parent hideshow 2 child comments replies: [–] SubArcticTundra@lemmy.ml 2 points 1 year ago* I can and I do *trollface* permalink fedilink source parent
[–] SubArcticTundra@lemmy.ml 2 points 1 year ago* I can and I do *trollface* permalink fedilink source parent
[–] eager_eagle@lemmy.world 33 points 1 year ago* (3 children) I work in an academic / research environment. Depending who wrote it, even seeing a __name__ == "__main__" is a bit of a rare thing... permalink fedilink source parent hideshow 6 child comments replies: [–] SpaceNoodle@lemmy.world 23 points 1 year ago (1 child) Academic code is absolutely horrific. Fortunately, it is possible to translate it for practical applications. permalink fedilink source parent hideshow 2 child comments replies: [–] arbitrary_sarcasm@lemmy.world 13 points 1 year ago As someone in academia who writes code, I can confirm. permalink fedilink source parent [–] HK65@sopuli.xyz 16 points 1 year ago (1 child) Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope? permalink fedilink source parent hideshow 2 child comments replies: [–] eager_eagle@lemmy.world 15 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] HK65@sopuli.xyz 11 points 1 year ago 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. permalink fedilink source parent [–] brian@programming.dev 4 points 1 year ago python isn't the only language to do "execute everything imported from a particular file and all top level statements get run". both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I'm sure there's more. python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn't hurt either. for instance in node this is the equivalent, even though I've never seen someone try before: if (path.resolve(url.fileURLToPath(import.meta.url)).includes(path.resolve(process.argv[1]))) { // main things } permalink fedilink source parent
[–] SpaceNoodle@lemmy.world 23 points 1 year ago (1 child) Academic code is absolutely horrific. Fortunately, it is possible to translate it for practical applications. permalink fedilink source parent hideshow 2 child comments replies: [–] arbitrary_sarcasm@lemmy.world 13 points 1 year ago As someone in academia who writes code, I can confirm. permalink fedilink source parent
[–] arbitrary_sarcasm@lemmy.world 13 points 1 year ago As someone in academia who writes code, I can confirm. permalink fedilink source parent
[–] HK65@sopuli.xyz 16 points 1 year ago (1 child) Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope? permalink fedilink source parent hideshow 2 child comments replies: [–] eager_eagle@lemmy.world 15 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] HK65@sopuli.xyz 11 points 1 year ago 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. permalink fedilink source parent
[–] eager_eagle@lemmy.world 15 points 1 year ago (1 child) 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. permalink fedilink source parent hideshow 2 child comments replies: [–] HK65@sopuli.xyz 11 points 1 year ago 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. permalink fedilink source parent
[–] HK65@sopuli.xyz 11 points 1 year ago 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. permalink fedilink source parent
[–] brian@programming.dev 4 points 1 year ago python isn't the only language to do "execute everything imported from a particular file and all top level statements get run". both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I'm sure there's more. python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn't hurt either. for instance in node this is the equivalent, even though I've never seen someone try before: if (path.resolve(url.fileURLToPath(import.meta.url)).includes(path.resolve(process.argv[1]))) { // main things } permalink fedilink source parent
[–] wise_pancake@lemmy.ca 15 points 1 year ago 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. permalink fedilink source parent
[–] NeatNit@discuss.tchncs.de 10 points 1 year ago I definitely do for quick scripts, but I try to break this habit. The biggest advantage of def main() is that variables are local and not accessible to other functions defined in the same script, which can sometimes help catch bugs or typos. permalink fedilink source parent
[–] LeninOnAPrayer@lemm.ee 8 points 1 year ago* 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() permalink fedilink source parent
[–] Anomalocaris@lemm.ee 2 points 1 year ago you can, no one stopping you permalink fedilink source parent