Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python global var #9

Open
patrickjohncyh opened this issue Feb 18, 2018 · 1 comment
Open

Python global var #9

patrickjohncyh opened this issue Feb 18, 2018 · 1 comment

Comments

@patrickjohncyh
Copy link

Do we declare a global variable within a function only if the variable is used? It appears that since we do not need to handle shadowing, it doesn't really matter if we declare extra global variables within a python function definition.

@m8pple
Copy link
Contributor

m8pple commented Feb 22, 2018

As you say, because shadowing won't occur (for this deliverable), the declaration of
a global variable in a function that doesn't use it shouldn't matter - it has no impact
on the function, and isn't actually detectable at run-time.

So if you find it more convenient (and it is quite likely you will, hence your question :) ),
the feel free.

In some ways this is similar to the approach used for languages like C for header
files. If you want to use fprintf, you need to #include <stdio.h>, but this actually brings
in declarations for loads of other functions as well. However, this should be safe, because
according to the C standard identifiers coming from the C library are reserved.
So a C compliant program should not define things called exit, or atoi, and there
is no possibility of aliasing/shadowing. (If you do bare-metal/embedded programming
then this changes, as you often end being the person defining things like malloc
and putchar, because there is no C library available).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants