Is my Python code a mess? [closed]
There are two files in the scope of this problem, let's call them file1.py
and file2.py
.
I've imported file2.py
into file1.py
.
I instantiate a class in file1.py
and I'd like 开发者_C百科to access it in file2.py
. I've tried referencing the object directly (just the class name) but that doesn't work. How can I reference the object? I don't want to move this to the same file, it'd make it messy.
Is there a non-hackish way to do this? If not, have I implemented a faulty design?
You need to pass the class from somewhere in File1.py to one of the classes in file.2.
You can simply pass a reference to the class when you invoke the method in file2.py where you need to use it.
Or you can write a "Set...." Method and pass the reference and store it in one of the classes in file2.py.
You could also make it a global static variable -- but you would get refused entry to the better coding clubs if you did this.
no, it's not a mess, i just think you're accessing the object the wrong way
first, import the file in file1.py
import file2
then, call the object without forgetting the scope:
file2.object.method
if you want a direct call to object.method, your import must change
from file2 import object
then you can do
object.method
read throu this link to understand more about modules
精彩评论