G
Gnarlodious
Is there a way to declare a project-wide variable and use that in all
downstream modules?
-- Gnarlir
downstream modules?
-- Gnarlir
Is there a way to declare a project-wide variable and use that in all
downstream modules?
Gnarlodious said:I'm doing that:
import Module.Data as Data
However I end up doing it in every submodule, so it seems a little
redundant. I wish I could load the variable in the parent program and
have it be available in all submodules. Am I missing something?
I'm doing that:
import Module.Data as Data
However I end up doing it in every submodule, so it seems a little
redundant. I wish I could load the variable in the parent program and
have it be available in all submodules. Am I missing something?
You can modify the builtin namespace:
But I don't think it's a good idea.
Is there a way to declare a project-wide variable and use that in all
downstream modules?
-- Gnarlir
What about using an environment variable?
OK I get it, LOL.from Module import Data
There, you saved three more characters .
Thanks, now I know what that means.But I don't think it's a good idea. Remember that "explicit is better than
implicit".
Yes, that's fine, but only if the data is suitable for it.
Unicode string
I'm doing that:
import Module.Data as Data
However I end up doing it in every submodule, so it seems a little
redundant. I wish I could load the variable in the parent program and
have it be available in all submodules. Am I missing something?
from test import ftest,itest
def test_main():
if __name__ == '__main__':
test_main()
I don't understand this. Can you explain, or refer me to some
documentation?
Seems like it should already be invented.
Let me restate my question.
Say I have a script Executable.py that calls all other scripts and
controls them:
#!/usr/local/bin/python
from Module import Data
import ModuleTest
ModuleTest.py has this:
print(Data.Plist.Structure)
Running Executable.py gives me this:
NameError: name 'Data' is not defined
1) Can I tell Executable.py to share Data with ModuleTest.py?
or if that can't be done:
2) Can I tell ModuleTest.py to "look upstream" for Data?
After the import is complete, yes.
import ModuleTest
ModuleTest.Data = Data
This works if the use of Data is inside a function that is not called
during import, not if the use of Data is at toplevel or in a class
statement outside a def.
That works! The solution looks like this:
# controlling program:
from Module import Data
import ModuleTest
ModuleTest.Data = Data
ModuleTest.getData()
# module:
def getData():
print(Data.Plist.Structure)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.