-2

Is it possible to declare a global variable that is invisible to the importing script?

For example, in script a.py I have a variable var_a that is accessible to any function in a.py. However, in script b.py that imports a.py, I want var_a to be inaccessible.

(A somewhat similar concept to C's static module variables)

In a.py:

var_a = "hello"

print("In script a.py:", var_a)

In b.py:

from a import var_a

print("In script b.py:", var_a)

Testing:

$ python3 b.py
In script a.py: hello
In script b.py: hello

I would like to get an error when referencing var_a from b.py.

3
  • 3
    possible to declare a global variable that is invisible to the importing script No. Commented Mar 7 at 17:18
  • 2
    There's no good way to do this, and practically every attempt can be trivially subverted by anyone who wants to. That's why in Python you just prepend the variable name with a single-underscore, e.g _var_a and that tells everyone "don't touch this, and if you do, it's your own fault if anything goes wrong" Commented Mar 7 at 18:50
  • 2
    This question is similar to: What does __all__ mean in Python?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 6 at 12:35

1 Answer 1

2

Another way also not with a "global" variable: Put the whole module's code in a function so it's in that function's scope, and run and delete that function. Declare as global the things you do want to offer.

Demo a.py, with an extra function that can be imported and used:

def scope():
    var_a = "hello"

    print("In script a.py:", var_a)

    global func_a
    def func_a():
        print("In func_a:", var_a)

scope()
del scope

Now in b.py you can do this:

from a import func_a
func_a()

But not this:

from a import var_a
print("In script b.py:", var_a)

Attempt This Online!

If you want to assign to the variable inside a function, then declare it nonlocal. For example:

    global func_a
    def func_a():
        nonlocal var_a
        var_a += ' again'
        print("In func_a:", var_a)

Attempt This Online!

Sign up to request clarification or add additional context in comments.

13 Comments

I already deleted my comment because I didn't read the code carefully enough. Note also that you don't need global unless you have multiple functions like scope() that share the variable.
And if any of the nested variables assign var_a, they need nonlocal func_a.
@Barmar That global is needed for b.py being able to use func_a. Usually modules aren't imported just for executing them but for then using what they offer. So I added that little example.
Ah, I thought it was global var_a.
well, you could have return func_a instead of making it global, but in any case... print(func_a.__closure__[0].cell_contents)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.