2

I cannot quite find a good description on how import works when importing your own files.

I was having trouble importing a file with a global variable and managed to get it to work when I put the global variable just before the files main function.

Can someone explain why it works this way?

A quick run down on how import actually works.

It did not work when I did this (pseudocode):

file1:

import file2 
file2.main()

file2:

main():
 glob_var = 0
 def add():
 global glob_var
 glob_var += 1
 add()

But worked if I put the variable first like this:

file1:

import file2 
file2.main()

file2:

glob_var = 0
main(): 
 def add():
 global glob_var
 glob_var += 1
 add()
asked Oct 23, 2015 at 8:39
1
  • 1
    This is nothing to do with importing. You need to define the actual global variable before you can refer to it via the global keyword. Commented Oct 23, 2015 at 8:46

4 Answers 4

2

'main' is just a method. Variable inside a method is local by definition. Thats why 2nd way is working.

answered Oct 23, 2015 at 9:00
Sign up to request clarification or add additional context in comments.

Comments

1

The reason it didn't work is because you are declaring the global variable inside of main. (you did miss the colon after definition of main which makes it confusing but looking at the indentation I suppose it's a definition). Global variables have to be defined outside the scope of any local function. This is a case of nested function definition.

You can do without global variables as well if that's what you are looking for. If however you want to use the variable defined in main inside a nested function then you can do the following: In python 3 there is a way to get this thing done however using the nonlocal keyword

def main():
 var = 10
 def nested_fun():
 nonlocal var
 var = var + 1

As you see we do not need a global variable here.

Edit: In case of python 2 this does not work. However you can use a list in the main and modify that list inside nested function.

def main():
 var = [10]
 def nested_fun():
 nonlocal var
 var[0] = var[0] + 1
answered Oct 23, 2015 at 9:02

Comments

0

If I understand the question correctly, it's regarding the global variable and have nothing to do with importing.

If you want to define a global variable, it has to be defined in module level. The main function is not the global scope, that's why the first code does not work as expected. By moving the variable declaration outside of main, it would be defined in global scope, so the add method can access it using as a global variable.

answered Oct 23, 2015 at 9:10

Comments

0

I think we have to begin with what global statement means. From the docs:

It means that the listed identifiers are to be interpreted as globals.

The important point is it's only about interpretation, i.e. global does not create anything. This is the reason, why your first example does not work. You are trying to add +1 to something not existing. OTOH, global_var = 100 would work there and it would create a new global variable.

answered Oct 23, 2015 at 12:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.