modules. a module is a file containing python definitions and statements intended for use in other...

Post on 29-Dec-2015

228 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Modules

Modules

• A module is a file containing Python definitions and statements intended for use in other Python programs.

• There are many modules as part of standard library.

• Example : the turtle module and the string modules.

Random numbers

We often use random number in programs.• To play a game of chance to pick a number,

throw some dice, flip a coin.• To shuffle a deck of playing cards randomly• To simulate possible rainfall .• For encryting banking sessions on the

Internets.

Module random

randrange method

• The randrange method call generates an integer between its lower and upper argument. Using the same semantics as range – so the lower bound is included, but the upper bound is excluded.

• The results are uniformly distributed.• Like range, randrange can also take an

optional step argument.

Shuffle a list

The time module

The

mat

h m

odul

e

Create modules

Namespaces

• A namespace is a collection of identifiers that belong to a module, or a function, or a class)

Namespace

Namespace

Namespaces in functions

Scope and lookup rules

• The scope of an identifier is the region of program code in which the identifier can be accessed, or used.

Scope and lookup rules

There are three important scopes in Python.• Local scope• Global scope• Built-in scope

Local scope

• Local scope refers to identifiers declared within a function.

• These identifiers are kept in the namespace that belongs to the function, and each function has its own namespace.

Global scope

• Global scope refers to all the identifiers declared within the current module, or file.

Built-in scope

• Built-in scope refers to all the identifiers built into Python- those like range and min that can be used without having to import anything, and are always available.

Precedence rules

• The same name could occur in more than one of these scopes.

• But the innermost, or local scope, will always take precedence over the global scope.

• The global scope always get used in preference to the built-in scope.

What gets printed?Our own one, or the built-in one?

Using the scope lookup rules determine this:

• Our own range function, Not the built-in function, is called.

• Because our range function is in global namespace, which take precedence over the built-in names.

What gets printed?Our own one, or the built-in one?

• So although names like range and min are built-in, they can be “hidden” from your use if you choose to defines your own variables or functions that reuse those names

Scope : more complex example

Attributes and the dot operator

• Variables defines inside a module are called attributed of the module.

• Attributes are accessed using the dot operator(.)

• The university and name attribute of moduleA and moduleB is accessed using moduleA.university

moduleB.name

top related