最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to make script-level variables private to the script - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Mar 7 at 17:13 ysapysap 8,12510 gold badges66 silver badges134 bronze badges 4
  • 3 possible to declare a global variable that is invisible to the importing script No. – John Gordon Commented Mar 7 at 17:18
  • @nocomment - Thanks. Care to add some useful details? – ysap Commented Mar 7 at 17:29
  • 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" – juanpa.arrivillaga Commented Mar 7 at 18:50
  • 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. – Christoph Rackwitz Commented Apr 6 at 12:35
Add a comment  | 

1 Answer 1

Reset to default 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!

发布评论

评论列表(0)

  1. 暂无评论