[Python] Efficient import

Discussão em 'Python' iniciado por Stack, Abril 4, 2025.

  1. Stack

    Stack Membro Participativo

    [edit] This question doesn't realy make sens and this situation could be avoided, being caused by a lack of effort from my part. Feel free to downvote to close.

    I have two files : file0 which has a object Obj containing variables and file1 which needs these variables. Here is the code:

    file0 :

    class A :
    def __init__(self) :
    self.v0 = 0
    self.v1 = 1
    Obj = A()


    file1 :

    from file1 import Obj
    v0 = Obj.v0
    v1 = Obj.v1
    del Obj
    print(v0, v1)


    Is there a more efficient way to do this. I have tried from file0 import Obj.v0 as v0 and from file0.Obj import v0 but I get errors for both. Would someone now a better solution ? By better I mean a solution that imports directly v0 and v1 without Obj. I prefer not to declare Obj in file1 because file0 is the main file doing most of the logic, but file1 needs to acces them. I cannot just use a variable in each file that I declare with the same value. I could merge the two files but for readability reasons, it wouldn't be practical.

    Continue reading...

Compartilhe esta Página