r/learnpython • • Nov 05 '20

How does one learn to make well-structured programs? Mine are dumpster-fires.

So I just (kind-of) finished reading Automate The Boring Stuff With Python, and for the past month I've felt like a big-brained super-hero automating myself out of a job (Data Analyst).

The problem is, I feel I missing something along the way. I don't understand the proper way to use functions, what a class is, why people have multiple files in a program, how/why people use GitHub...probably some other things I haven't discovered yet.

They all seem to be related to organization? Is there a "How To Structure Your Python Stuff With Python" book?

478 Upvotes

98 comments sorted by

View all comments

5

u/PercyJackson235 Nov 05 '20

Well, functions are, nominally, for any large enough chunk of code that you are going to reuse, either in the current program or in other program, without having to rewrite it. A class is a blueprint for creating an object that is a grouping of data and functions, that are in this instance called methods. Multiple files in a program is for logical separation. People will put data, functions, classes, etc.. that go logical together in the same file. GitHub is used for version control, collaboration, and, as a byproduct, backups. It allows you to keep a full history of a project, make and test changes without making a real impact on the project, and facilitates multiple people working on a project at once.

5

u/TheGrapez Nov 05 '20

Thank you for this explanation!

I feel like my issue with functions also comes with the fact that I'm not yet at the point where I need to worry about the structure. Most of my "programs" are just small scripts that do a single thing.

I now feel compelled to take on a larger project, so I can practice logical separation.

1

u/gmorf33 Nov 05 '20

That could be it. Your script might be small enough that it itself could be considered a function in a larger piece of code.

Functions, as a general rule, should do one thing and have little to no side effects beyond that one thing the function is doing. They are also for parts of your code that you would want to call more than once.

A couple of example functions:

A csv row formatter. It takes in a row (as a string) from a CSV file, and it returns a row with some formatting changes (as a string).

An error collector: It takes a logfile path as an argument, searches the log file for "ERROR" entries, and returns a list of strings with all the errors.

A phone number formatter: It takes phone numbers (as a string) and returns them in a normalized format (as a string).

With all of these examples, you have specific tasks each function handles with no side effects on the rest of your code. They are reusable in any part of your code base because they take arguments, which allows you to call them any time you need that functionality, without rewriting all the code. Like these phone number format calls:

format_phone_num('555.555.5555') -> '1 (555)-555-5555'
format_phone_num('5556666666') -> '1 (555)-666-6666'

You can keep calling it and passing whatever argument you want, and never have to rewrite the code that's doing the actual formatting.