[Python] How to share an instance for classes as class variable? [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a way to share an instance of a class as a class variable?

    I tried this, but it said that NameError: name '_Unit1__global_object_in_module' is not defined.

    __global_object_in_module = SharedObject()

    class Unit1:
    ...
    global __global_object_in_module
    _gl_object = __global_object_in_module
    ...

    class Unit2:
    ...
    global __global_object_in_module
    _gl_object = __global_object_in_module
    ...


    Because I thought that it caused the __global_object_in_module variable to be regarded as a class variable, but I'm not sure how to resolve this. Any help would be appreciated.

    Environment

    • python 3.10

    Continue reading...

Compartilhe esta Página