SoFunction
Updated on 2024-11-14

Python object-oriented modules in detail

1.1 Introducing modules

  • import module name
  • form module nameimport function name
  • form module name import *
  • import module name as alias
  • import module name import function name as alias

The following is verified with sqrt under the math module

1.1.1 import module name

grammatical

 # 1 Importing modules
 import module name  # This format is recommended
 import module name1,module name2.... # No pushing and shoving
   # 2 call
 module name.functional name()

experience for oneself

	import math
	print((9))

1.1.2 from module name import function name

vocabulary

# 1 Import
from module name import functionality1,functionality2....
# 2 call
functionality()

experience for oneself

	from sqrt import math
	print(sqrt(9))

1.1.3 from …import*

grammatical

from module name import*

experience for oneself

	from math import*
	print(sqrt(9))

1.1.4 as Defining aliases

To change complex and long module names that are hard to remember into simple ones.

grammatical

# Module definition aliases
import module name as nickname
# Function definition aliases
from module name import functionality as nickname

experience for oneself

import math as  m
print((9))
from math import srqt as s
print(s(9))

coding

# --*coding:utf-8*--
# Requirements Verify sqrt() squaring calculations under the math module
"""
Steps
1 Import the math module
2 Verify sqrt function : call sqrt function under this module
"""
# Method 1 : import module name; module name. Function
import math
print((9))
# Method 2 :from module name import function,function... ;function call(function())
from math import sqrt
print(sqrt(6))
# Method 3 :from module name import* ;call function
from math import *
print(sqrt(16))
# Method 4 - Module Alias: import module name as alias; call function Alias. function
import math as mm
print((16))
# Method IV-functional alias: from math import sqrt as ss
from math import sqrt as ss
print(ss(9))

1.2 Production modules

In Python, each Python file can be used as a module, and the name of the module is the name of the file.

Custom module names must conform to identifier naming rules

goal

  • corresponds English -ity, -ism, -ization
  • Production steps and writing code
  • Mastering the Do's and Don'ts

Why make a module.

Programmers in the development, for some frequent use of the code, packaging, production into a module to meet the needs of the payroll after the call!

1.2.1 Defining modules

Create a new Python file named my_module1.py and define the testA function.

	# Here: Explanatory text related to this module, e.g. a function to add any two numbers.
	def testA(a,b):
		print(a+b)

1.2.2 Test modules

After writing a module, test it for bugs.

In practice, when a developer writes a module, in order to make the module can achieve the desired effect in the project, the developer will add some test information in the py file, such as in my_module1.py to add test code.

def testA(a,b):
	print(a+b)
testA(5,6)			

In this case, both the current file, and any other file that imports the module, will execute the testA function call at runtime. In particular, other function calls will call the test function as well, which we don't want.

The solution is as follows.

With the introduction of the name system variable, the code above becomes

# --*coding:utf-8*--
# Requirement: A function to add any two numbers.
def testA(a,b):
    print(a+b)
# Test information
# text(5,6) # validate1 This will be executed when other functions are called.
"""
 validate (a theory)2  print(__name__) 
 test (machinery etc) Under the current function,and when called by other documents,Return results:
 inside (part, section)  come (or go) back  __main__  
 externally  come (or go) back The name of this function 
"""
# Do if-judgment based on the __name__ feature.
if __name__ == "__main__":
    testA(2,5)
"""=====exports==When called by itself 执行test (machinery etc)语句=="""    
7 

1.2.3 External calls

# --*coding:utf-8*--
# Guide packages
import my_module1
# Call Module Name. Function
my_module1.testA(6,12)
"""==exports==The external call is No test code execution=="""
18

1.3 Module Positioning Sequence

When importing a module, the Python parser searches for the location of the module in order of proximity.

  • current module
  • If not, search each directory under the shell variable PYTHONPATH.
  • Not yet, Python will look at the default path (which varies from system to system), e.g. under unix, the default path is /usr/local/lib/Python.

** PYTHONPATH is the default directory for the installer.

take note of

  • Do not duplicate the name of an existing module with your own filename, otherwise the module will not work.
  • When importing a function from a module name, if the function name is duplicated, whoever comes after it will take effect.

1.4 all

goal

  • What is an __all__ list
  • corresponds English -ity, -ism, -ization
  • Write Code Analyze Role Experience

What is all list is a variable in the module that takes the form of a list [].

Role.If you import a module with __all__ in it using from module name import *, you can only import the elements in the list after the all.

my_module1 module code

___all__=["testA]
def testA():
	print("I'm testA.")
def testB():
	print("I'm testB.")

The result of the execution is :

I'm testA.

If there are no __all__=[] variables

The result will be: all all

I'm testA.
I'm testB.

summarize

That's all for this post, I hope it was helpful and I hope you'll check back for more from me!