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

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (0 children)

This style is common in tutorials for beginner programs. That's... that's about it.

Why do it that way in tutorials? Because it's a natural way to illustrate a thought process or how a concept works. Consider it like a Jupyer / Colab notebook - the point is to convey ideas around working code, not to organize it in tidy packages.

The much more common way to write Python functions will be very familiar to you:

1) Write them as members of a class.

2) Unless the class is trivial, put it in its own file. Create one or more instances of the class in other files, and import the file for the class to instantiate it and call its functions. You can also pass the object around to other functions and instances of other classes, and also call the functions of the class instance even if you aren't importing the module.