[Python Learning/Tutorial 2] - Function Module

Python provides a set of technologies that can be easily shared, including modules and some development tools:

Modules allow you to organize your code for optimal sharing.

Publishing tools allow you to share your modules with the world.

We can take the function from the previous section

def print_lol(the_list):

     for movie in the_list:
        if isinstance(movie,list):

           print_lol(movie)

        else :

            print(movie)

Write this into text and change the suffix to .py


The Python Package Index (PyPI) provides a centralized repository for third-party Python modules on the Internet. Once you're ready, use PyPI to publish your module, making your code available to others.

Before uploading, this code is not complete because it lacks necessary comments .

In Python, multiline comments are

"""                 """

For example:

""" def print_lol(the_list):

     for movie in the_list:
        if isinstance(movie,list):

           print_lol(movie)

        else :

            print(movie) """

# comment for a single line

#定义了movies变量
movies=["The Holy Grail",1975,"The Life of Brian",1979,["Graham Chapman",["Michael Paline","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]]]

"""
这个函数是用来循环遍历列表中的列表的。
"""
def print_lol(the_list):

     for movie in the_list:
        if isinstance(movie,list):

           print_lol(movie)

        else :

            print(movie)
		
#调用上面的函数
print_lol(movies)

Click on this .py file to run it directly (it will disappear in a flash, and it will not stop after outputting.)

ready to release

    In Python, a "distribution" is a collection of files that join together to allow you to build, package, and distribute your module.

    1: First create a new folder for the module, and put the .py file into the folder

    2: Create a file called setup.py in the new folder.

            This file contains metadata about the publication. Edit this file and add the following code:

#从Python发布工具导入setup函数
from distutils.core import setup

setup(
	name		=	'test'
	version		=	'1.0.0'
	py_modules	=	['test']
	author		=	'紫极岚'
	author_email=  	'[email protected]'
	url			= 	'https://blog.csdn.net/qq_22570497'
	description =	'一个简单的遍历列表的函数'
)

build release

    3: Build a release file.

    The release tools contain all the functionality needed to build a release. Open a terminal window in the folder,

    Type the one line command python setup.py sdist

    (If you want to run this statement, you must first configure the python environment variable, that is, add it after the path of the environment variable ; your python installation path     is fine.)

    4: Install the distribution into your local copy of Python

         python setup.py install

import the module and use

import test

Use the python keyword Import to introduce, and the suffix .py does not need to be added after the test

The import statement tells Python to include the test.py module in the program. Then we can use the functions in this module

>>> import test
>>> 
movies=["The Holy Grail",1975,"The Life of Brian",1979,
["Graham Chapman",
["Michael Paline","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]
]]
>>> print_lol(movies)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print_lol(movies)
NameError: name 'print_lol' is not defined

It seems that the result is not what we thought, what is going on?


Python modules implement namespaces

All code in Python has a namespace association.

Code in the main Python program (and in the IDLE shell) is associated with a namespace called _main_. When you put code in its own module, Python automatically creates a namespace with the same name as the module. So, the code in your module will be associated with a namespace called test.

We can call it with test.print_lol(movies)

from test import print_lol This will add the specified function to the current namespace, so that the call does not need to add the previous namespace.

Special attention needs to be paid here if there is a print_lol function in your original code, then the imported function will overwrite your original function.

The final version of the Python module

#定义了movies变量
movies=["The Holy Grail",1975,"The Life of Brian",1979,["Graham Chapman",["Michael Paline","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]]]

"""
这个函数是用来循环遍历列表中的列表的。
如果def print_lol(the_list,level):直接这么写的话level参数是必须的
这样更新代码会导致API更新,不传level就不能用
写成level=0这个level参数就不是必填的了。
如果level为负数,则关闭缩进功能
"""
#这里注意False的F要大写
def print_lol(the_list,flag=False,level=0):
	for movie in the_list:
		if isinstance(movie,list):
			print_lol(movie,flag,level+1)
		else:
			if flag:
				for num in range(level):
				#tab换行,tab的个数为level的个数
					print("\t",end='')
				print(movie)
			else:
				print(movie)
		
#调用上面的函数
print_lol(movies)





Guess you like

Origin blog.csdn.net/qq_22570497/article/details/80745204