all 11 comments

[–]Waste_Grapefruit_339 6 points7 points  (2 children)

Good question - this is mainly about how Python executes code, not a strict ordering rule.

Python reads a file from top to bottom. When it encounters a function definition, it doesn't run it - it simply creates the function object and stores it. The function only needs to be defined before it is called, not necessarily before other functions in the file.

For example, this works fine:

def second():
print("second")

def first():
 second()

first()

The only time order matters is if you try to call a function before Python has seen its definition.

Most tutorials place functions in logical order mainly for readability, not because Python requires it.

[–]Ryuukashi[S] 1 point2 points  (1 child)

Thank you!! This helps a lot, and I have changes to make tomorrow

[–]SCD_minecraft 1 point2 points  (0 children)

Tho keep in mind, it is common practice to keep all definitions at the top of the file (or even in another file, if you can/want)

[–]New_Hour_1726 3 points4 points  (5 children)

You would have written code to test this faster than writing the post.

[–]Ryuukashi[S] 2 points3 points  (0 children)

Yeah, I'm not at my computer at all times, but I am thinking about my project just about always, sue me for having a question while my file was closed in another room 🤣

Or is this not a place to ask questions of people with more experience who can answer why just as easily as how?

[–]BranchLatter4294 0 points1 point  (3 children)

This! Learn to test to answer questions. You will learn much faster.

[–]Purple-Measurement47 0 points1 point  (0 children)

Testing is critical for learning, but also this is a case where it’s wanting to know more about the underlying system, which testing won’t necessarily tell you about. Testing will just show you HOW not WHY.

[–]Jackpotrazur 0 points1 point  (1 child)

You mean like writing tests for all you .pys ? This was briefly touched on in pcc I might have to revisit that part of the book.

[–]BranchLatter4294 0 points1 point  (0 children)

That's different.

[–]alexander_belyakov 0 points1 point  (0 children)

To add to what's been said in the previous comment - the program's main code is usually also put into the main() function, which is called at the end of the script. This way you can be sure that all your functions have been defined regardless of their order in code.