Learning python packages and modules

Here are my own experiments and excerpts from books


  • Note that the explanation given in the third paragraph above may be a little confused, simply put__ doc__ Only the first string can be output (the following "the first line... The second line is an empty line..." refers to a multi line string, which is wrapped in three quotation marks). This string can be a multi line string. The logical line roughly means the first line at the beginning of the code (the blank line is not the line, and the remaining line is the first line). The experiment is given below!
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ cat mm.py 
import package

print(package.module1.__doc__)
print(package.module1.printf.__doc__)	# Note that this is not printf()__ doc__


(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ cat package/module1.py 



"""hello

f afa
s f
a
f a
f
a
f
a

"""
def printf():

	"""
	Used to output
	lsfaklklas 
	fasklfhsjafkhjkhsf
	hsdajkhfajsfhak
	hsjafkhjk
	"""
	print("in module1")
print("scv jiu xu ")
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ 

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ python mm.py 
scv jiu xu 
in __init__.py
hello

f afa
s f
a
f a
f
a
f
a


	Used to output
	lsfaklklas 
	fasklfhsjafkhjkhsf
	hsdajkhfajsfhak
	hsjafkhjk



Note that when importing packages, only__ init__. If the global code in py runs, other modules in the package will not run the global code because of importing the package, but if__ init__.py, only the global code in the imported module will be run.
__ init__.py did not import other modules

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cat module2.py 
def printf():
	print("in module2")
printf()
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cat __init__.py 
# from . import module1
# from . import module2
# module1 and module2 are not imported here

print("in __init__.py")
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cd ../
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ ls
mm.py  package
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ python mm.py 
in __init__.py

__ init__.py has imported other modules!

base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cat __init__.py 
from . import module1
from . import module2
# Two other modules are added

print("in __init__.py")
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cat module1.py 
def printf():
	print("in module1")
printf()
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cat module2.py 
def printf():
	print("in module2")
printf()
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study/package$ cd ../
(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ python mm.py 
in module1
in module2
in __init__.py

But if__ init__. If no other modules are imported into py, use mm. Above Py file as an example, we can't use package The use of module1

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ cat package/__init__.py 
# from . import module1
# from . import module2


print("in __init__.py")

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ python mm.py 
in __init__.py
Traceback (most recent call last):
  File "mm.py", line 3, in <module>
    package.module1.pritnf()
AttributeError: module 'package' has no attribute 'module1'

At this time, change directly__ init__. The PY file is as follows

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ cat package/module1.py 
def printf():
	print("in module1")
print("scv jiu xu ")

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ cat package/__init__.py 
from . import module1
# from . import module2
print("in __init__.py")

(base) skt1faker@SKT1Faker:~/my_procedure/python/package_study$ python mm.py 
scv jiu xu 
in __init__.py
in module1

*List all members in the current module
*Enumerate the members of the module
*Members enumerated as instances
*Enumerate members of a class

Namespace

Anywhere in a Python program, there are several namespaces available.
1. Each function has its own namespace, called local namespace, which records the function variables, including function parameters and locally defined variables.
2. Each module has its own namespace, called global namespace, which records the variables of the module, including functions, classes, other imported modules, module level variables and constants.
3. There is also a built-in namespace, which can be accessed by any module. It stores built-in functions and exceptions.

Namespace lookup order

  • Search order of variables
    When a line of code wants to use the value of variable x, Python will look for variables in all available namespaces in the following order:
    1. Local namespace: refers specifically to the methods of the current function or class. If the function defines a local variable x, or a parameter x, Python uses it and stops the search.
    2. Global namespace: refers specifically to the current module. If the module defines a variable, function, or class named x, Python will use it and stop the search.
    3. Built in namespace: Global for each module. As a final attempt, Python will assume that x is a built-in function or variable.
    4. If Python cannot find x in these namespaces, it will abandon the search and throw a NameError exception, such as NameError: name 'aa' is not defined.

  • Search order of functions: (note that functions can be nested, and another function can be defined in one function, so there is the name of the parent function)
    1. Search the namespace of the current (nested or lambda) function first
    2. Then search in the namespace of the parent function
    3. This is followed by a search in the module namespace
    4. Finally, search in the built-in namespace

The explanation of namespace article It's very good. I also refer to part of him

reference resources:

  1. Basic course of python Programming budget method, Jiang Hong, Yu Qingsong, Tsinghua University Press
  2. The essence of Python namespace - Xiao Shao running
  3. Python Basics (2): doc, docString, help() -- happy mortal 721

Keywords: Python Back-end

Added by john_nyc on Mon, 10 Jan 2022 15:53:58 +0200