简体   繁体   中英

How to import global variables in python from a class module in another file?

I have a file that contains the class definitions and functions I need to use in my main file to make the text cleaner. However, I'm having a problem with imported global variables.

There is plenty of information at SO and other resources regarding how to make function variables global within the same code or how to use the global variables from an imported file. However, there is no information on how to access a variable from an imported file if the variable belongs to a function belonging to a class.

I would appreciate any help on how to do it or why it cannot be done. Please skip the lecture on the dangers of using global variables like this as my situation requires such use.

Edit: Sorry for not having an example in the original post. It's my first one. Below is an example of what I'm trying to accomplish.

Let's say I have a file classes.py that contains:

class HelixTools():
    def calc_angle(v1, v2):
    v1_mag = np.linalg.norm(v1)
    v2_mag = np.linalg.norm(v2)

    global v1_v2_dot
    v1_v2_dot = np.dot(v1,v2)
    return v1_v2_dot

Then in my main text file I do:

from classes import HelixTools

ht = HelixTools()
v1 = some vector
v2 = some other vector
ht.calc_angle(v1,v2)
print(v1_v2_dot)

The result is "v1_v2_dot" not defined. I need v1_v2_dot to use it as the input of another function.

Here's an example of how you can access class attributes (if I understand what it is you want to do correctly). Lets imagine you have a python file called "Test_class.py" that contains the following code:

class Foo(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def bar(self):
        self.z = self.x + self.y

Now lets imagine you want to import this class into another python file in the same directory, and access attributes of that class. You would do this:

from Test_class import Foo

# Initialize two Foo objects
test1 = Foo(5, 6)
test2 = Foo(2, 3)

# Access the x and y attributes from the first Foo object
print(test1.x)  # This will print 5
print(test1.y)  # This will print 6

# Access the x and y attributes from the second Foo object
print(test2.x)  # This will print 2
print(test2.y)  # This will print 3

# Access the z attribute from the first Foo object  
test1.bar()
print(test1.z)  # This will print 11

# Access the z attribute from the second Foo object
test2.bar()
print(test2.z)  # This will print 5

This works because variables defined in the __init__ magic method are initialized as soon as the Foo object is first called, so the attributes defined here can be access immediately after. The bar() method has to be called before you can access the z attribute. I made 2 Foo objects just to show the importance of including "self." in front of your variables, in that each attribute is specific to that particular class instance.

I hope that answers your question, but it would be very helpful if you provided some example code to show exactly what it is you want to do.

You should likely use a class attribute to store this value. Note that the implementation will depend on what your class HelixTools really does. But for the example, you could use something like this:

import numpy as np

class HelixTools():

    def __init__(self):
        # Initialize the attribute so you'll never get an error calling it
        self.v1_v2_dot = None

    def calc_angle(self, v1, v2):       # Pass self as first argument to this method
        v1_mag = np.linalg.norm(v1)
        v2_mag = np.linalg.norm(v2)
        # Store the value in the attribute
        self.v1_v2_dot = np.dot(v1,v2)

And then:

from classes import HelixTools

ht = HelixTools()
v1 = some vector
v2 = some other vector
ht.calc_angle(v1,v2)    # This will not return anything
print(ht.v1_v2_dot)     # Access the calculated value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM