Python core programming Second Edition 326th page 12th chapter exercises-Python core programming answers-self-developed-

Source: Internet
Author: User

This is a self-made exercise and may be incorrect. You are welcome to discuss and discuss various optimization and reconstruction solutions.

12-1.
Path search and search path. What is the difference between path search and search path.
[Answer]
The Path Search refers to the operation to Search for a file (the pursuit of a file ), the latter is to find a group of directories (through a set of directories ). This article is from balian
The default search path is specified during compilation or installation. For more information about the current search path, see the following example. You can use the append () method of the List to add a search path.

>>> import sys>>> sys.path['', 'D:\\Python27\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python27\\lib\\site-packages']>>>

 

12-2.

Import attributes. Assume that your module mymodule contains a foo () function.

(A) How can I import this function to your namespace?

(B) What are the differences between the two imported namespaces?

[Answer]

(A) Use the import Statement (import module) and from-import Statement (from module import ). Avoid using the from module import * Statement.
(B) Using the from-import Statement, foo () is directly imported to a local namespace, so it must be used directly without the limitation of the module name.

If we use the path () function in the sys module of Question 12-1 as an example:

The code for using the first method (import module) is as follows:

>>> Import sys >>> sys. path # The limitation of the module name must be added: ['', 'd: \ Python27 \ python27.zip ', 'd: \ Python27 \ DLLs', 'd: \ Python27 \ lib ', 'd :\\ Python27 \ lib \ plat-win', 'd :\\ Python27 \ lib-tk ', 'd: \ python27', 'd: \ Python27 \ lib \ site-packages '] >>> path # note the error here, path () traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'path' is not defined >>>

The code for using the second method (from module import) is as follows:

>>> From sys import path >>> sys. path # main error here. The module name cannot be added with the Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sys 'is not defined # This article is from the blog Park balian >>> path # path () must directly use ['', 'd: \ Python27 \ python27.zip ', 'd: \ Python27 \ DLLs ', 'd: \ Python27 \ lib', 'd: \ Python27 \ lib \ plat-win ', 'd: \ Python27 \ lib-tk ', 'd: \ Python27', 'd: \ Python27 \ lib \ site-packages '] >>>

 

[Reference]

Differences between the two import methods of the python Module

Http://developer.51cto.com/art/201003/189555.htm

Three methods for importing python modules

Http://www.docin.com/p-56218303.html

12-3.

What is the difference between importing "import module" and "from module import?

[Answer]

"From module import *" can import all the names of the specified module to the current namespace, but this usage must be limited. See the 317 page of this book.

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.