In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how Python uses the module. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1.1 Import module
Import module name
Form module name import feature name
Form module name import *
Import module name as alias
Import module name import feature name as alias
The following is verified by sqrt under the math module
1.1.1 import module name
Grammar
# 1 Import module import module name # this format is recommended for import module name 1, module name 2. # does not push the # 2 calling module name. Function name ()
Experience
Import math print (math.sqrt (9)) 1.1.2 from module name import feature name
Grammar
# 1 Import from module name import function 1, function 2. # 2 call function ()
Experience
From sqrt import math print (sqrt (9)) 1.1.3 from... Import*
Grammar
From module name import*
Experience
From math import* print (sqrt (9)) 1.1.4 as definition alias
As the complex module name that is too long to remember into a simple one.
Grammar
# module definition alias import module name as alias # feature definition alias from module name import function as alias
Experience
Import math as mprint (m.srqt (9) from math import srqt as sprint (s (9))
Code
#-* coding:utf-8*--# requirements Verification sqrt () squared calculation under math module step 1 Import math module 2 verify sqrt function: call the sqrt function under this module "" # method 1: import module name; module name. Function import mathprint (math.sqrt (9)) # method 2: from module name import function, function...; function call (function ()) from math import sqrtprint (sqrt (6)) # method III: from module name import*; call function from math import* print (sqrt (16)) # method IV-module alias: import module name as alias; call function alias. Function import math as mmprint (mm.sqrt (16)) # method 4-function alias: from math import sqrt as ssfrom math import sqrt as ssprint (ss (9)) 1.2 production module
In Python, each Python file can be used as a module, and the name of the module is the file name.
Custom module names must conform to the naming rules for identifiers
target
Action
Production steps and writing code
Master matters needing attention
Why do you make a module:
In the process of development, programmers encapsulate some frequently used code and make it into a module that meets the needs, and then call it in the later salary.
1.2.1 define the module
Create a new Python file, name my_module1.py, and define the testA function
# here: explanatory text related to this module, such as a function that completes the addition of any two numbers def testA (aformab): print (aquib) 1.2.2 test module
After writing a module, test whether there is bug in it.
In the actual development, when a developer writes a module, in order to make the module achieve the desired effect in the project, the developer will add some test information to the py file, such as adding test code to my_module1.py.
Def testA (aformab): print (axib) testA (5pr 6)
At this point, no matter the current file or other files that import the module, the call to the testA function will be executed at run time. In particular, other function calls will also call the test function, which we do not want to see.
The solution is as follows:
By introducing the name system variable, the above code becomes
#-- * coding:utf-8*--# requirement: a function completes the addition of any two numbers def testA (aform b): print (aforb) # test information # text (5) 6) # verify 1 this will be performed when other functions are called, validate 2 print (_ _ name__) tests under the current function, and when called by other files Return result: internal return _ _ main__ external return this function name "" # according to _ _ name__ attribute Do if to judge if _ _ name__ = "_ _ main__": testA (2Magazine 5) "" = output = = execute the test statement when you call yourself = "71.2.3 external call #-* coding:utf-8*--# guide package import my_module1# call module name. Function my_module1.testA (6p12) "" = = output = = external calls do not execute test code = "181.3 module positioning order
When importing a module, the Python parser searches for the location of the module in the order from near to far
Current module
Currently, search each directory under the shell variable PYTHONPATH
Not yet. Python will check the default path (different systems vary). For example, under unix, the default path is / usr/local/lib/Python.
* * PYTHONPATH installer is the default directory for
Be careful
Do not duplicate your own file name with the existing module name, otherwise the module function will not be available.
When using the from module name import function, if the function name is repeated, who will function after it.
1.4 all
target
What is a _ _ all__ list
Action
Writing code analysis function experience
What is an all list is a variable in a module, and the value is in the form of a list []
Function: when there is _ _ all__ in the module, if you import using the from module name import *, you can only import the elements listed after this all.
My_module1 module code
_ _ all__= ["testA] def testA (): print (" I am testA ") def testB (): print (" I am testB ")
The result of the execution is:
I'm testA.
If there is no _ _ all__= [] variable
The result of the execution will be: all-owned
I'm testA.
I'm testB.
Thank you for reading! This is the end of this article on "how to use the module of Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.