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

31
1 Outline 7.1 Introduction 7.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 Destructors 7.7 Class Attributes 7.8 Composition: Object References as Members of Classes 7.9 Data Abstraction and Information Hiding 7.10 Software Reusability
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    0

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

Page 1: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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

Page 2: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

2

Python, short Python, short introductionintroduction

Python, short Python, short introductionintroduction

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

Page 3: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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.

Page 4: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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.

Page 5: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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.

Page 6: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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.

Page 7: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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' ...

Page 8: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

8

Control Flow – for statement

Page 9: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

9

More control statements

Page 10: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

10

Defining Functions

Page 11: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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

Page 12: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

12

Data Structures

Page 13: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

13

Page 14: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

14

Page 15: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

15

Page 16: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

16

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

tutorial!

Page 17: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

17

Page 18: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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()

Page 19: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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)

Page 20: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

20

Standard Modules

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

Page 21: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

21

Packages

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

Page 22: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

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 *

Page 23: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

23

Classes

Page 24: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

24

Page 25: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

25

Standard Library of Python

Page 26: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

26

Page 27: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

27

Page 28: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

28

Page 29: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

29

Python CGI (1)

Page 30: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

30

Python CGI (2)

Page 31: 1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.

31

Python CGI (3)