1

I'm using latest python version; I have a simple function in one file, then another file calls that function. Problem is the variable from function isn't printed.

file1.py:

var = "one"
def first():
    global var
    if smt == True:
        var = "1"
    else:
        var = "W"

file2.py:

from file1 import *
first()
print(var)

This is simplified version because I have more irrelevant code, but the problem is still the same, my variable doesn't change for some reason.

Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34
webnub
  • 41
  • 7

1 Answers1

1

The practice of using import * is usually discouraged; due to the fact that it might be prone to namespace collisions, inefficient if the import is huge et cetera.

I would personally go for an explicit import: from file1 import first

I also believe that you have the wrong idea of what global is. This might help:

In the first case the global keyword is pointless, so that is not correct. Defining a variable on the module level makes it a global variable, you don't need to global keyword.

The second example is correct usage.

However, the most common usage for global variables are without using the global keyword anywhere. The global keyword is needed only if you want to reassign the global variables in the function/method.

Keep in mind that you do not have var in file2.py by simply using global keyword; if you'd like to access the variable var you can use something like:

In file1.py:

var = "one"


def first():
    global var

    var = "1"

In file2.py:

import file1 

file1.first()
print(file1.var)
Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34
  • I tired that but then i get undefined variable if i want to use the one that was used in my function. – webnub Oct 17 '18 at 07:26
  • Answer updated; a global variable, is not shared across packages. - You need to use the example shown in my answer. – Julian Camilleri Oct 17 '18 at 07:28
  • One funny thing was when I used `id()` on `var` in both files I got the same value. I cannot understand why. Thanks for this ans, I learned something. A related qn is https://stackoverflow.com/q/3338283/6561141 – mrtpk Oct 17 '18 at 07:38
  • I get "Statement seems to have no effect" for file1.var – webnub Oct 17 '18 at 07:40
  • 1
    @tpk do you mind sharing how you used `id()`; as in on which variable; since you don't have `var` in `file1` unless you import the file and reference the variable; which in that case would be the same instance of the variable. – Julian Camilleri Oct 17 '18 at 07:46
  • @webnub I updated the answer. - Try it again, although that warning comes from IntelliJ as far as I'm aware. – Julian Camilleri Oct 17 '18 at 07:51
  • Thanks, this works now. I tried to use var in file2 but kept getting undefined value and your last edit showed me why: i tried to use "var" when i needed to use "file1.var" when using it from that point :D – webnub Oct 17 '18 at 08:01
  • Glad it helped buddy. – Julian Camilleri Oct 17 '18 at 09:43
  • @JulianCamilleri I was using `id()` on `var` in file1 and file2. I imported just as in the question. – mrtpk Oct 17 '18 at 09:45
  • They're not the same, if you try a small experiment: `id(var)` and `id(file1.var)` you'll see that both of which have different object identities. – Julian Camilleri Oct 17 '18 at 10:16