#๐ Functions
8 messages ยท Page 1 of 1 (latest)
@hybrid solar
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
When a function is defined, the Python bytecode compiler determines how to fetch a variable x that appears in it, based on these rules:
- If there is a global x declaration, x comes from and is assigned to the x global variable module.
- If there is a nonlocal x declaration, x comes from and is assigned to the x local variable of the nearest surrounding function where x is defined.
- If x is a parameter or is assigned a value in the function body, then x is the local variable.
- If x is referenced but is not assigned and is not a parameter:
- x will be looked up in the local scopes of the surrounding function bodies (nonlocal scopes).
- If not found in surrounding scopes, it will be read from the module global scope.
- If not found in the global scope, it will be read from
__builtins__.__dict__.
I think it's usually a good idea to put the variables near where you're gonna use them. Like, if you stick num1 and num2 inside the function, it could get confusing later on, you know? So, I'd say keeping them up top makes things clearer and easier to manage, I guess.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.