Basically I have a application that sends a value to another function(located in another file) and I want other functions(within that file) to have access(if it matters, its read access) to that variable.
For example:
def function1(pricelist):
#do something
…. other functions
def fucntion200():
#Does something else but needs to refer to that pricelist that was provided to function1
I tried doing a pricelist = pricelist in function1 so maybe if I declare it in the file it’ll be avail, but that didn’t work and when I make it global I get an error saying its local and global.
I have read: Using global variables in a function other than the one that created them and don’t think it fully applies(or I have so far been unable to).
Any ideas?
Thanks!
It really depends on the control flow of your application. What is calling function1 and is it the one that ends up calling function200? At the very basic level, your function200 would take a pricelist arg as well, and modify it in place:
It would be most desirable to have one module using the other as a utility to where it can call the functions with the params it needs. Whatever is calling function1 could theoretically have access to function200 and call it as well.
Using module globals should probably be reserved for constant values or things that aren’t going to be modified by a bunch of unknown sources, but it would be something like:
moduleA.py
moduleB.py
But if all you are doing is writing top level functions, then they should all take parameters to operate on, and optionally return new versions or modify in place.