Global Variable and Function in Python giving Error

Viewed 136

I have two variables x and y defined in the main loop of the Python program and I created a separate module and imported that module into the Python main. But I am still getting error from the module as it say the NameError: name 'x' is not defined.

The following is the main routine:

from addition import add
x=5
y=2

ans=add()

the module addition is as follows:

def add():

    global x,y
    return x+y

Also, do I have to redeclare the library in the separate module such as numpy? since this library is already imported in the main program and I feel re-importing is quite redundant.


Update:

importing x,y into addition module give following error:

ImportError: cannot import name 'add' from partially initialized module 'addition' (most likely due to a circular import)

It only works if I do this:

x=5
y=2
def add():
   x+y
print(add())

So why not other way round?

0 Answers
Related