Python Core Programming Second Edition 12th Chapter after class answer

Source: Internet
Author: User

12-1. Path search and search path. What is the difference between a path search and a search path?

The former refers to the operation of finding a file, which is to find a set of directories.

12-2.

Import properties. Suppose you have a Foo () function in your module mymodule.

(a) What are the two methods of importing this function into your namespace?

(b) What is the difference between the two methods of importing namespaces?

Import MyModule  from Import foo
The first is to import all the attributes in MyModule, and the second imports only Foo

 

12-3. What is the difference between importing "import module" and "From Module import*"?

* is directly imported into the local namespace, so it can be used directly, without the need to add the module name qualification. You can import a standalone item or use the FROM module import * to import everything.

12-4. Namespaces and variable scopes. What is the difference between namespaces and variable scopes?

A namespace (namespaces) is a mapping of names (identifiers) to objects. The namespaces are divided into local namespaces (local namespaces), Global Namespaces (Globals Namespaces), and built-in namespaces (Build-in namespaces).
Build-in namespaces is first loaded into the active namespaces by the Python interpreter, which is made up of the names in the __buildins__ module.
Global namespaces is then loaded as an active namespaces, and you can use the Globals () built-in function to determine whether a name belongs to the global namespace.
Local namespaces when needed, that is, if a function is called during execution, it is loaded as an active namespaces, and you can use the locals () built-in function to determine whether a name belongs to a local namespace.
The scope of a variable (identifier) is defined as the scope of its declaration within the program (scope). See page 295 of this book. Variables defined within a function have a local scope, whereas variables of the highest level in a module have global scope.
Note that the names of all local spaces are within the local scope. All names outside the local scope are within the global scope.
Local namespaces and scopes change with function calls, and the global namespace does not change.

The following figure is from the original book. Added a Chinese comment, and some modifications. For reference only.

#本文来自博客园balian

Python Core Programming second Edition 12th after class answer

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.