"head First python" 2. Share your code function module

Source: Internet
Author: User

#Coding:utf-8#Comment code! #Add two comments, a description module, a description function" "This is the nester.py module, which provides a function called Print_lol (), which functions as a printed list, which may or may not contain nested lists. " "defprint_lol (the_list):" "This function takes a positional parameter named the_list, which can be any Python list (or a list that contains nested lists), and each item of the specified list is returned (recursively) to the screen, one row for each data item. " "     forIteminchthe_list:ifisinstance (item,list): print_lol (item)Else:            Printitemprint_lol (movies)

The module is a text file that contains Python code, ending with A. py.

#try to use theMovies = Movies = ['the Holy Grail', 1975,'The Life of Brain', 1979,          ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]print_lol (movies)

"ready to publish"

1. Create a folder for the module Nester

2. Create a setup.py file in the folder

#Coding:utf-8 fromDistutils.coreImportSetup#Import the Setup function from the Python publishing tool#these are the parameters of the Setup functionSetup (name='Nester', Version='1.0.0', Py_moudles=['Nester'], Author='Hetao', Author_email='[email protected]', the URL='https://home.cnblogs.com/u/Archimedes/', Description='A Simple printer of nested lists',    )

Build a Publication

1. Build a publishing file

2. Install the publication to a Python local copy

There are two new folders in the Nester folder that are created by the publishing Tool.

"import the module and use the"

Python 2.7 (r27:82525, Jul 4, 09:01:59) [MSC v.1500 + bit (Intel)] on Win32
Type "copyright", "credits" or "license ()" for more Information.
>>> Import Nester
>>>

You can see that the import was successful and then try to use it

Python 2.7 (r27:82525, Jul 4, 09:01:59) [MSC v.1500 32bit (Intel)] on Win32type"Copyright","credits" or "License ()"  formore Information.>>>ImportNester>>> movies = Movies = ['the Holy Grail', 1975,'The Life of Brain', 1979,          ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]>>>print_lol (movies) Traceback (most Recent call last): File"<pyshell#2>", Line 1,inch<module>print_lol (movies) nameerror:name'print_lol'  is  notdefined #python没有找到你的函数!!! >>>

That's What we're going to recognize as a "module namespace."

Namespace I is like a last name, the correct way to use is Nester.print_lo (movies)

>>> nester.print_lol (movies) the Holy Grail1975 the Life ofBrain1979Graham Chapmanmichael Palinjohn cleeseterry gilliameric Idle

That would be good.

Note: If you use the From Nester import print_lol, You can add the specified function to the current namespace, which means that the namespace is not qualified with the Nester.

This is not advocated, however, because at this point it is important to note that if a function print_lol with the same name is already defined in the current namespace, then this function will be covered by the introduction of the function to cause Trouble.

"register PyPI website"

To publish on pypi, you must first register an account (https://pypi.python.org)

"" P49 ""

"head First python" 2. Share your code function module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.