1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special...

Preview:

Citation preview

1

Outline7.1 Introduction7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4 Controlling Access to Attributes

7.4.1 Get and Set Methods 7.4.2 Private Attributes

7.5 Using Default Arguments With Constructors 7.6 Destructors7.7 Class Attributes 7.8 Composition: Object References as Members of Classes 7.9 Data Abstraction and Information Hiding 7.10 Software Reusability

2

Python, short Python, short introductionintroduction

Python, short Python, short introductionintroduction

CSC 667/867CSC 667/867San Francisco State Univ.San Francisco State Univ.

3

Abstract• Python Tutorials at http://docs.python.org/tut/• Python is an easy to learn, powerful programming

language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python's elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

• The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.

• For a description of standard objects and modules, see the Python Library Reference document. The Python Reference Manual gives a more formal definition of the language.

4

Features and Strength I• You could write a Unix shell script or Windows

batch files for some of these tasks, but shell scripts are best at moving around files and changing text data, not well-suited for GUI applications or games. You could write a C/C++/Java program, but it can take a lot of development time to get even a first-draft program. Python is simpler to use, available on Windows, MacOS X, and Unix operating systems, and will help you get the job done more quickly.

• Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than shell scripts or batch files can offer.

5

Features and Strength II

• high-level data types built in, such as flexible arrays and dictionaries

• Because of its more general data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.

• Built in modules -file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.

6

Features & Strength III• Python enables programs to be written

compactly and readably. – the high-level data types allow you to

express complex operations in a single statement;

– statement grouping is done by indentation instead of beginning and ending brackets;

– no variable or argument declarations are necessary.

7

Control Flow – if statement

>>> x = int(raw_input("Please enter an integer: ")) >>> if x < 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More' ...

8

Control Flow – for statement

9

More control statements

10

Defining Functions

11

Defining Function II

There are more information to read about functions related to scoping, arguments, etc. Check “4.7 More on Defining Functions” from the tutorial

12

Data Structures

13

14

15

16

More data structures• Tuples• Sequences• Sets• Dictionaries• So, where to look at? Check the

tutorial!

17

18

Modules• A module is a file containing Python definitions and

statements. • The file name is the module name with the suffix .py

appended. • If fibo.py contains fib() function and fib2() function,

then user can invoke them using– Import fibo– fibo.fib()– fibo.fib2()

• Or– from fibo import fib, fib2 – fib()– fib2()

19

Modules• The module Search Path using

environment variable, PYTHONPATH– on Unix, this is usually .:/usr/local/lib/python

• ``Compiled'' Python files (for speeding up the modules)– .pyc files– .pyo files (optimized compilation is used)

20

Standard Modules

• Python Library Reference at http://docs.python.org/lib/lib.html

21

Packages

• Packages are a way of structuring Python's module namespace by using ``dotted module names''.

22

Packages• When importing the package, Python searches

through the directories on sys.path looking for the package subdirectory.

• The __init__.py files are required to make Python treat the directories as containing packages;

• Users of the package can import individual modules from the package, for example: – import Sound.Effects.echo– Sound.Effects.echo.echofilter(input, output,

delay=0.7, atten=4)

• An alternative way of importing the submodule is: – from Sound.Effects import echo – from Sound.Effects import *

23

Classes

24

25

Standard Library of Python

26

27

28

29

Python CGI (1)

30

Python CGI (2)

31

Python CGI (3)

Recommended