Jennifer Jennifer - 1 year ago 35
Python Question

How to import dict initialized in init constructor from one python file to another?

I am trying to figure out how to import my dictionary in a python file to another dictionary. When I try:

from filename import myDict

I get "ImportError: cannot import name myDict" and I imagine this is because my dictionary is initialized as follows:

def __init__(self):
self.myDict = {}

I have also tried:

from filename import self.myDict

And then I get a syntax error that says the "." in self.myDict is invalid. If anyone can provide any suggestions on how to resolve this issue it would be greatly appreciated! Thanks.


Something like this?

class X:
    def __init__(self):
        self.myDict = {}

from filename import X
print X().myDict