0

I want to do something similar to that outlined here:

Global Variable from a different file Python

However I believe that I am not getting an output from the variable file.foo because foo is not explicitly defined in file.py. Foo is an array of matrices created from a readfile. file.py appends values from the readfile to foo thus file.py must run independently to define foo.

Because of this, I am wondering if there is a way to dump the definition of the python variable to some location that I can call from later. My other alternative is to dump this variable to a txt file which I really dont want to do to avoid extraneous file creation and other complications.

Edit:

This is what I have:

color_matrices = Full_Layout(array=org_arr, resolution=1)

def Globalize_RPF():
     global color_matrices

Then I am trying to call color_matrices in my current pyfile like so:

 from ThreeD_Core_Distribution import color_matrices
 RPF = color_matrices
 print(RPF)
Community
  • 1
  • 1
Sterling Butters
  • 1,024
  • 3
  • 20
  • 41

1 Answers1

0

I post this answer but I'm not quite really sure if I understood your question. You referred to this question: Global Variable from a different file Python

However I believe that I am not getting an output from the variable file.foo because foo is not explicitly defined in file.py. foo is an array of matrices created from a readfile.

You referred to file.foo as not explicitly defined in file. What I understand from your statement is that, foo lives within another object that lives in file module. So in order to get foo you need to fetch it through that object:

import file 
file.object.foo

or:

import file *
object.foo

file.py appends values from the readfile to foo thus file.py must run independently to define foo.

You would basically import file module in order to append the values from the readfile. Hence, within file you would open the readfile to append its values to foo which is basically an object of file: file.foo.

I just hope that I got your what you're asking about.

Edit:

Let's suppose the code you posted is in a module file called file:

color_matrices = Full_Layout(array=org_arr, resolution=1)

def Globalize_RPF():
    global color_matrices

color_matrices is global because it's assigned at the top-level of the module. This statement global color_matrices inside Globalize_RPF function will declare that you intend to change the global variable color_matrices:

def Globalize_RPF():
    color_matrices = Full_Layout(array=org_arr, resolution=1)  # Local to function, you cannot use `file.foo` after importing `file` module  

When assigned this way, color_matrices is local to the function. What does that mean? It means the variable won't be accessible outside the function. So even if you try to use color_matrices after the function in file module, it won't work.

color_matrices = Full_Layout(array=org_arr, resolution=1)      # Top-level assignment, global var

def Globalize_RPF():
    # process color_matrices

Now color_matrices is a global variable, that's you can use it inside file module in functions, classes, in expressions and many other places, you can reference the variable anywhere in file because its global now. Because color_matrices is global now and not a local variable inside a function, we can get its value by importing file like the following:

 from file import color_matrices
 RPF = color_matrices
 print(RPF)

If you need to re-assign color_matrices to new results inside the function, use global keyword:

def Globalize_RPF():
    global color_matrices
    color_matrices = ...          # Changes global color_matrices

If you use only color_matrices = ... inside the function body you'll simply create color_matrices that's local to the function and you'll shadow/override color_matrices of the global scope, the one that's assigned at the top of the module.

GIZ
  • 4,409
  • 1
  • 24
  • 43
  • So maybe thats the problem: foo does not live within an object in the file module. Rather it is simply a variable defined by a custom function that parses the readfile. So when I attempt to print(file.foo) in my current pyfile, nothing is returned. Maybe having foo live in an object is the solution? I am not an expert in python so Im not sure I would know how to do this. – Sterling Butters Apr 21 '17 at 20:29
  • Ok, since `foo` is a local variable in a function, it's accessible by that function and any enclosing functions if there's any. In order to make `foo` accessible when you qualify its name through the module name: `file.foo`, you need to move the assignment to the global scope. That's, either you declare `foo` global within the function like: `global foo` and then you assign whatever values to `foo`: `foo = ...` within the function or you move the assignment of `foo` outside the function, at the top-level of the module. – GIZ Apr 21 '17 at 20:36
  • please see edit, I clearly dont know what i am doing – Sterling Butters Apr 21 '17 at 20:58
  • I dont get any errors in what I am doing but it may also be worth noting that when I add " from ThreeD_Core_Distribution import color_matrices" to my current pyfile, nothing is printable. Why is this? – Sterling Butters Apr 21 '17 at 21:04
  • Thank you, I think I understand better now. I originally had the top level assignment in your third code block and I do not intend to change color_matrices in the current pyfile so I guess I do not need the function in your first code block. The problem now is, I am doing exactly what your 3rd and 4th code blocks say (without the function in 3rd) and I cannot get an output for RPF. In fact, nothing will print as long as "from file import color_matrices" is in my code – Sterling Butters Apr 21 '17 at 21:59
  • @SterlingButters Have you tried to print `color_matrices` directly: `print(name_matrices)?` And make sure you initialized `Full_Layout` object correctly. If nothing prints and no exception was raised, I believe you're having a problem with your object or perhaps another name rebinding somewhere in your file to a same-named variable. – GIZ Apr 22 '17 at 07:35