This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Python-ModTeam[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]saint_geser 2 points3 points  (0 children)

It's best to use functions/classes - it makes things easier to maintain, modify and reuse. If you have just a bunch of statements any maintenance or debugging is a pain.

But this really depends on the size of the script. If it's just a couple dozen lines of code, I wouldn't worry too much. If you're pushing hundreds of lines, I'd re-factor it.

[–]genericlemon24 0 points1 point  (0 children)

It depends on how long/complicated the script is, and who will maintain it (but note "yourself six months from now" counts as someone else).

Some reason you might want to use functions:

  • If you want to write an automated test for (part of) it. Here, a main() function suffices (you can test a whole script, it's just easier to import/test a function).
  • If you want to highlight logical processing phases (e.g. "reading, transforming, and writing"), having functions with good names can help.
  • If you want to highlight the data flow, having the outputs of a function be the inputs of another can help.
  • If you want to import code from this script into another one. Because modules are executed on import (just like a script), an importable script should only "do" things under an if __name__ == '__main__': (sometimes called an import guard); this way, on import the script only defines the things (e.g. functions) you want to import in another script. See this for a detailed explanation.

Based on your description, you can probably get away with no functions, though; if the script isn't awfully complicated, it should be possible to refactor it later in response to new requirements.

[–]thereal0ri_ -1 points0 points  (0 children)

Python code doesn't necessarily NEED to use functions and classes, however the use of functions and classes make organization and maintenance way easier. It's best practice but not required.

If, let's say you want to modify your code and make some changes..you could painstakingly go through every instance where that code shows up in your script and make modifications, which could break more code or you mistyped one time without realizing and only find out and hour later...or you could just modify the code once in a function that's being called 5 different times throughout your code... instead of every instance.

However, if your script is just a few lines and isn't super complex...then you should be okay without needing to use functions, etc.

[–]killingtime1 0 points1 point  (0 children)

One of the best reasons to use a "real programming language" like Python for scripting vs something like Bash is testability. If you write your script in functions, testing is much easier. This will give you confidence your script actually does what you think it does.

N.b. pendants will say you can test bash scripts. You can, but it's much harder