0

main1.py

import mya
a=10
mya.increment_a()
a=a-5
print(a)

module mya.py

def increment_a():
    global a

    a=a+1
    print(a)

name 'a' is not defined. I don't understand why. I declare variable a as global in module, so from this point a=0 as it is in mail1.py upd: I need work globaly. Starting value for variable "a" set in main.py, function in module mya.py will edit "a", and return new value to main.py for further use. --- closed topic--- Now I use "arguments" and "return" and in work:

mya.py

def increment_a(a):
    a=a+1
    print(a)
    return a

main1.py

import mya
a=0
print(a)
a=mya.increment_a(a)
a=a+10
print(a)
RedSubmarine
  • 177
  • 4
  • 16
  • 2
    Does this answer your question? [Visibility of global variables in imported modules](https://stackoverflow.com/questions/15959534/visibility-of-global-variables-in-imported-modules) – sammy Dec 06 '19 at 22:26
  • I need work with "a" globally. Starting value for variable "a" set in main.py, function in module mya.py will edit "a", and return new value to main.py. – RedSubmarine Dec 06 '19 at 22:54
  • @RedSubmarine no, you don't *need* to, and indeed, it is considered an anti-pattern to rely on global mutable state. Python itself doesn't support true global variables, global means "module level global". The best solution is to improve your design to avoid global mutable state – juanpa.arrivillaga Dec 06 '19 at 23:02
  • So only way to work globally is move all function from mya.py to main1.py? – RedSubmarine Dec 06 '19 at 23:05
  • 1
    @RedSubmarine or you could work directly on the module in `main1.py`, so `mya.a = foo` etc. But again, this is all a terrible practice. Don't do any of this. – juanpa.arrivillaga Dec 06 '19 at 23:10
  • Ok. Arguments and return worked: #---mya.py def increment_a(a): a=a+1 print(a) return a #---main1.py import mya a=0 print(a) a=mya.increment_a(a) a=a+10 print(a) – RedSubmarine Dec 06 '19 at 23:16

1 Answers1

0

From Visibility of global variables in imported modules:

Globals in Python are global to a module, not across all modules. (Many people are confused by this, because in, say, C, a global is the same across all implementation files unless you explicitly make it static.)

One approach to deal with it is:

import mya
mya.a=3
mya.increment_a()
sammy
  • 857
  • 5
  • 13
  • Since you identified that the question is a duplicate, there is no need for you to post an answer. – khelwood Dec 06 '19 at 22:36
  • I wasn't sure how this works, so just in case. Should I delete it? – sammy Dec 06 '19 at 22:38
  • I read all related topics and has no understanding of this situation. Yes this approach can fix error. But I need work with variable a not only in module. I need work globaly. Starting value for variable "a" set in main.py, function in module mya.py will edit "a", and return new value to main.py. For suggested approach it will not work. – RedSubmarine Dec 06 '19 at 22:53
  • @RedSubmarine the best solution is not to rely on global mutable state. So, either write your functions to take data in as arguments and return data as return values, or write a class to encapsulate state, and use instances of that class in your modules. – juanpa.arrivillaga Dec 06 '19 at 23:03
  • The linked post suggests some alternatives, like setting attributes in the namespace of a separately imported module `shared_stuff`, and have other modules import it, too. – sammy Dec 06 '19 at 23:12