![](/img/trans.png)
[英]Python Imports - Reference Top Level Module Instead of the “In-Level” Module
[英]Do not see module imports at top level of Python package
考慮以下python
包結構
working_directory/
-- test_run.py
-- mypackge/
---- __init__.py
---- file1.py
---- file2.py
並說在file1.py
里面我已經定義了一個函數func1()
,我還from numpy import array
了一些類似於from numpy import array
函數。 現在我想導入並使用test_run.py
mypackage
而不在命名空間中看到這些numpy函數。 我想使用import mypackage as mp
導入它並查看
mp.file1.func1()
mp.file2.func2()
etc
我不想看到mp.file1.array()
。 我該怎么做?
一種可能性是使用下划線:
from numpy import array as _array.
雖然這並不禁止人們訪問mp.file1._array
但通常認為以下划線開頭的變量是“私有的”。
AFAIK,沒有簡單的方法來禁止訪問python中的任何變量。 (一種方法是使其成為類的屬性,請參閱: https : //docs.python.org/3/library/functions.html#property )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.