Section 12.10. Exercises


12.10. Exercises

12-1.

PathSearch versus SearchPath. What is the difference between a path search and a search path?

12-2.

Importing Attributes. Assume you have a function called foo() in your module mymodule.

  1. What are the two ways of importing this function into your namespace for invocation?

  2. What are the namespace implications when choosing one over the other?

12-3.

Importing. What are the differences between using "import module" and "from module import *"?

12-4.

Namespaces versus Variable Scope. How are namespaces and variable scopes different from each other?

12-5.

Using __import__().

  1. Use __import__() to import a module into your namespace. What is the correct syntax you finally used to get it working?

  2. Same as above, but use __import__() to import only specific names from modules.

12-6.

Extended Import. Create a new function called importAs(). This function will import a module into your namespace, but with a name you specify, not its original name. For example, calling newname=importAs ('mymodule') will import the module mymodule, but the module and all its elements are accessible only as newname or newname.attr. This is the exact functionality provided by the new extended import syntax introduced in Python 2.0.

12-7.

Import Hooks. Study the import hooks mechanism provided for by the implementation of PEP 302. Implement your own import mechanism, which allows you to obfuscate your Python modules (encryption, bzip2, rot13, etc.) so that the interpreter can decode them properly and import them properly. You may wish to look at how it works with importing zip files (see Section 12.5.7).



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net