Python script calling another and passing all vars set in first
Python script calling another and passing all vars set in first
(OP)
I've seen many examples of python script A calling python script B, but in testing I can't figure out how ALL variables set in the script A can be referenced by the called script B. eg I set x = "foo" in the 1st script and then when I call the 2nd script, I want to reference x which has been set to "foo". If I have 50 + (an unknown # ) vars set in the first script, I don't want to explicitly hand the second script all 50+ variables since I can't really know what they are each time it runs, I just want it to script B to 'know' all 50+. Is that possible ? Some languages call this an 'include'. thanks !
RE: Python script calling another and passing all vars set in first
https://www.datacamp.com/community/tutorials/scope...
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
I don't know another language which does includes this way as just in place adding of the include file as if this was one script.
The only way this can make sense, also in PHP, is when this only happens conditionally and not always. If it's always it only may help with mainting code in two or more shorter files.
I would really think about refactoring the code to shorten it, make it OOP or at least procedural with several functions to which you pass the necessary vars.
For now the fastest solution you have is really just merging your two files into one, it's not good programming style anyway. My experience with such features only one or few languages offer is the feeling of superiority of this feature, but it really asks for bad programming style.
Actually the closest I know is exec(open("./filename").read()), but I don't use that. This will run the file in the same context, but I'm not sure whether that means having same variable access, what's for sure is it can access the same sys.argv arguments initially going onto the first py script call.
Chriss
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
A file containing Python statements.
A Python program.
Classes.
Functions.
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
I would try it as follows: At the end of the 1.script save the variables into a JSON-file and at the begin of the 2.script read the saved JSON into dictionary.
RE: Python script calling another and passing all vars set in first
RE: Python script calling another and passing all vars set in first
I'm totally aware. I asked whether his job is to do a conversion from PHP to Python.
Chriss