In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the common import guide package mode of python". In the daily operation, I believe that many people have doubts about the common import package guide mode of python. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "python common import package guide mode". Next, please follow the editor to study!
1. Control the contents of the module that are all imported
When using the 'from module import *' statement, you want precise control over the symbols exported from the module or package. Define a variable "_ all _" in your module to explicitly list what needs to be exported.
# somemodule.pydef spam (): passdef grok (): passblah = 4 "Only export 'spam' and' grok'__all__ = ['spam',' grok']
Although strongly opposed to the use of 'from module import *', it is frequently used in modules that define a large number of variable names. If you don't do anything, such an import will import everything that doesn't start with an underscore. On the other hand, if "_ all _" is defined, only what is listed will be exported.
If you define "_ _ all__" as an empty list, nothing will be imported. If "_ all _" contains an undefined name, it causes AttributeError on import.
two。 Split the module into multiple files
You want to split a module into multiple files. But you don't want to break existing code when you unify separate files into a single logical module.
Program modules can be split into separate files by turning them into packages. Consider the following simple modules:
# mymodule.pyclass A: def spam (self): print ('A.spam')
Class B (A): def bar (self): print ('B.bar')
Suppose you want to divide mymodule.py into two files, one class for each definition. To do this, first replace the file mymodule.py with the mymodule directory. In this directory, create the following files:
Mymodule/ _ _ init__.py a.py b.py
Insert the following code in the a.py file:
# a.pyclass A: def spam (self): print ('A.spam')
Insert the following code into the b.py file
# b.pyfrom .an import Aclass B (A): def bar (self): print ('B.bar')
Finally, in _ init _ .py, glue the two files together:
# _ _ init__.pyfrom .an import Afrom .b import B
If you follow these steps, the resulting package MyModule will act as a single logical module:
> import mymodule > a = mymodule.A () > a.spam () A.spam > > b = mymodule.B () > b.bar () B.bar > > at this point, the study of "common import guide packages for python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.