0

I cannot write into a global variable in the following program. Can someone please give me a solution to this? Note that my var variable must be in other file than Mod2.py and Mod3.py

In Mod1.py

var = 5

In Mod2.py

from Mod1 import *

def foo(newValue):
  global var
  print('foo: %d' % var)
  var = newValue

print('before: %d' % var)
foo(2)
print('after: %d' % var)

In Mod3.py

from Mod2 import *

foo(3)
print('var: %d' % var)

The result when running Mod3.py is

before: 5
foo: 5
after: 2
foo: 2
var: 2

But I expect it to be

before: 5
foo: 5
after: 2
foo: 2
var: 3

I do not want a solution using import Modx.py

kaosad
  • 1,233
  • 1
  • 11
  • 15

4 Answers4

2

It probably has to do with the fact that importing variables does not as you might think it does:

from Mod1 import var

is the same as

import Mod1
var = Mod1.var  # this creates a new variable

whereas I can't explain exactly what's going on in your code, I can say that by changing your imports to be:

import Mod1

and referring to the var (without global now) as

Mod1.var

Problem solved.

Mod2.py:

import Mod1

def foo(newValue):
  print('foo: %d' % Mod1.var)
  Mod1.var = newValue

print('before: %d' % Mod1.var)
foo(2)
print('after: %d' % Mod1.var)

Mod3.py:

import Mod1
from Mod2 import *

foo(3)
print('var: %d' % Mod1.var)

Output:

before: 5
foo: 5
after: 2
foo: 2
var: 3

Please also take some time to go through http://www.python.org/dev/peps/pep-0008/; a few of the things it recommends:

* avoid wildcard imports (`from <module> import *`) for the various problems they introduce
* module names should be `lowercase`

Furthermore, global variables are not usually considered good, and there's always a way around them; see Why are global variables evil?.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
0

I do not want a solution using import Modx.py

That's impossible. The only way to do what you want is to rebind the name in the other module, and that requires importing and accessing the module rather than the name directly.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

You need to say from mod1 import var.

Why?

Your code says from mod1 import, but if you look at this, you can tell it's an unfinished statement. What do you want it to import? You must specify. This is if you don't want to do the simpler process import mod1, which would import everything.

0

Yes, I have read an article elaborating at length the evil of mixins. In my case, the original code, containing interrelated functions (no class used), was too bloated that I have decided to split them into 2 modules (B.py, and C.py). However, the 2 access a few same variables, so I factored those variables out into A.py. Then I have D.py containing functions which are dependent on functions in B.py and C.py. The dependency graph is as shown below:

      A
    /   \
   B     C
    \   /
      D

(Bottom relies on the top)

I have no good reason to use mixins, it just started out with 1 and I thought I wanted to keep them in the same namespace even separated into four modules.

Just now I thought of a solution using getter and setter functions for each variable. But I ditched that idea because it was too clumsy.

kaosad
  • 1,233
  • 1
  • 11
  • 15
  • Why do you mixin Python modules? What you likely rather want to do is to mixin classes, isn't it? Then your global variables become instance (object) variables or - if really needed - class variables. And then it suddenly may make sense... – cfi Oct 08 '13 at 08:44
  • Btw, I don't see how this 'answer' is an answer to your question. This is not a discussion forum. Please read the FAQ. Thanks. – cfi Oct 08 '13 at 08:45