R
R
RUNAMAN2018-02-09 09:54:38
Python
RUNAMAN, 2018-02-09 09:54:38

Python. How to get rid of "doubling" of variables?

main.py

import add

b = 2

def main():
    global a
    a = 1
    add.show_test()

main()

add.py
def show_test():
    import main

    print('a', main.a)
    print('b', main.b)

>>>
a 1
b 2
a 1
b 2

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
longclaps, 2018-02-09
@longclaps

Cyclic imports are a sign of great intelligence.
You and the cards in hand)

K
Konstantin Malyarov, 2018-02-09
@Konstantin18ko

Are you seriously?
Import is done only in one file, and you import the first into the second, and the second into the first.
Or the first to the second, or the second to the first.

R
RUNAMAN, 2018-02-09
@RUNAMAN

Suggested by smart people. Maybe someone will come in handy ..

import add

b = 2

def main():
    global a
    a = 1
    add.show_test()

if __name__ == '__main__': 
    main()

import __main__ as core

def show_test():
    print('a', core.a)
    print('b', core.b)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question