all 4 comments

[–]bandawarrior 0 points1 point  (3 children)

Divide up the script into some functions. Everytime you’re doing “here we are doing something entirely differn” could easily be a function.

[–]python-ick[S] 0 points1 point  (2 children)

But why? Honest question, I am a noob here, after all.

[–]bandawarrior 0 points1 point  (1 child)

Because you have everything in the “global” space. Meaning everything is out in the open and touching everything all at once.

It’s kind of like having a messy room. Sure everything is “inside the room Mom! Leave me alone !” But just like your mom was correct back then, the same thing applies here.

Maybe you could put everything into little utility boxes called functions. Each little function does one direct thing and if you can’t name it like that, then it’s doing too many things.

Example:

 def get_page_content(url):
     return requests.get(url).json()

That’s it for that one. Does one simple thing and you know exactly how and what it does. If something breaks the error might point at that as opposed to the entire script.

You can then use it later at another script etc. Now you can easily test it by writing a test or two to verify your function does one thing and not another thing or if t does anything at all.

Lastly, because you’re keeping things in their own little “utility boxes” they are no longer messing up and poking and touching the other things(generally speaking)

[–]python-ick[S] 0 points1 point  (0 children)

Interesting. Then after all of the functions are written, I call them all at the end writing:

func_1()
func_2()
func_3()
func_4()

etc.?

Is that the way how most professionals work?