[Python] import global variable from an other file inside class in Python [closed]

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    i have problem with my global variable that needs to be set in a function (f1 in my example) and recovered as is in a second one (f2). the value of the global is set by f0, witch is not editable. I have to send global n value with f0

    in my test i did in a single file

    class mytest():
    def f1(value):
    global n
    n = value
    print('test f1 : '+n)

    def f2():
    print('test f2 : '+n)


    def f0():
    mytest.f1("5")
    mytest.f2()
    f0()


    result is 5 for f1 and f2, as expected. now i split in 2 files (to reproduce my need. no choice here, it must be splitted)

    file1

    def f0():
    file2.mytest.f1("5")


    file2

    class mytest:
    def f1(value):
    global n
    n = value
    print('test f1 : '+n)

    def f2():
    print('test f2 : '+n)


    f2()


    error is : name 'n' is not defined

    f1 is always run first by the app, then f2 is called later I need f2 (and fX functions i need behind) to keep 'n' value from f1 i have read similar questions Accessing global variables from inside python functions

    How can I access global variable inside class in Python

    but those doesn't answer my question thank you

    Continue reading...

Compartilhe esta Página