Python (2.7): how to avoid imported modules to appear as part of my modules? -


I've written two modules m1.py and m2.py, each of which uses different modules from the standard library.

For example

  # m1.py Import System # #.   

and # m2.py Import OS #. #.

What's "botherful" to me that when I import into main.py, then two modules m1.py and m2.py I use the functions defined in sys and os like this Can I:

  # main.py print m1.sys.version print m2.os.listdir ()   

whether it is normal, or any import Should I consider some modules in my code?

Usually you do not need to worry about what is accessible in your namespace, which is a stuff that That the documents in the module are not part of the API, they are screwed around, whatever problems they receive, they are qualified. Python assumes that these programmers are responsible adults. The exception occurs when you want to allow other codes to be otherwise used with otherwise discouraged mymodule import * syntax then whatever you want to do public, so that you can create your own internal content Do not mess with your importer's name space.

Here's how you can do this:

  • The name that starts with an underscore (like _foo ) Is considered private, and will not be imported from mymodule import * to . This is not "real" privacy, because whichever is normal, it will still be able to access them via mymodule._foo (but they probably should not!).

  • If you want to make the module private, use a like section to name it as "personal" as described above. . I.e., use _os as imported OS

  • Or instead of roaming with underscores, you can use a __ all __ < Can create code / variable which explicitly lists the public names of the module. Only names in the list will be imported from a mymodule import * statement. Note that if any __ is required in all __ sequence packages, if you want to import the submodules from mypackage import * . The reason for this is that Python can not trust the file system because the files contained in the package are not confused with the capitalization.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -