12. Modules - 12 MODULES Rocky K C Chang November 6...

Info icon This preview shows pages 1–10. Sign up to view the full content.

12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and Richard Enbody. 2012. The Practice of Computing Using Python 2nd Edition . Addison Wesley.)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Objectives Explain the specification of modules. Become familiar with the use of docstrings in Python. Explain the use of modules and namespaces in Python. Describe and use the different forms of module import in Python. Explain the local, global, and built-in namespaces and the scope resolution.
Image of page 2
Where do the modules come from? The term “module” refers to the design and/or implementation of specific functionality to be incorporated into a program. Modular-II MODULE Hello; FROM STextIO IMPORT WriteString; BEGIN WriteString("Hello World!"); END Hello. A Python module is a file containing Python definitions and statements. By convention, modules are named using all lower case letters and optional underscore characters.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

EXERCISE 12.1 Import a module. Use help(module_name) or dir(module_name) to find out the functions available in that module. Use print(module_name) to find out what the module does. Use print(function_name.__doc__) to print out what the function does.
Image of page 4
Interface, client and docstring A module’s interface is a specification of what it provides and how it is to be used. Any program code making use of a given module is called a client of the module. A docstring is a string literal denoted by triple quotes used in Python for providing the specification of certain program elements.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

EXERCISE 12.2 Try Import the graphics module dir(graphics) help(graphics) print(graphics.__doc__)
Image of page 6
Documentation String (docstring) ( - strings) A docstring is a string literal denoted by triple quotes given as the first line of certain program elements. These additional lines must be indented at the same level. The docstring for a module is simply the first block of quoted text to appear in the module. Write docstrings for all public modules, functions, classes, and methods. Convention: """Return a foobang Optional plotz says to frobnicate the bizbaz first. """
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

EXERCISE 12.3 Write docstrings for your previous assignment.
Image of page 8
EXERCISE 12.4 Run graphics.py as a main module.
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 10
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '16
  • Rocky Chiang
  • Local variable

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern