unauthorized evaluation - itcourseware...2018/07/24  · table of contents rev. 1.0 chapter 7:...

64
Python 3 for Scientists EVALUATION COPY Unauthorized reproduction or distribution is prohibitied

Upload: others

Post on 28-Mar-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Python 3 for Scientists

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 2: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Python 3 for Scientists

Table of Contents

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 3: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 4: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Table of ContentsAbout this Course

Welcome................................................................................................................ ATC-1

Classroom etiquette............................................................................................... ATC-2

Course outline........................................................................................................ATC-3

Student files........................................................................................................... ATC-4

Extracting the student files.................................................................................... ATC-5

Examples............................................................................................................... ATC-6

Lab exercises......................................................................................................... ATC-7

Appendices............................................................................................................ ATC-8

Chapter 1: The Python environment

Chapter 1 Objectives...................................................................................................1-1

Starting Python............................................................................................................1-2

If the interpreter is not in your PATHs........................................................................ 1-3

Using the interpreter....................................................................................................1-4

Trying out a few commands........................................................................................1-5

The help() command................................................................................................... 1-6

Running a Python script..............................................................................................1-7

Python scripts on Unix................................................................................................1-8

Python scripts on Windows.........................................................................................1-9

Python editors and IDEs........................................................................................... 1-10

Python 3 for Scientists TOC-1© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 5: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Table of Contents rev. 1.0

Chapter 2: Getting Started

Chapter 2 Objectives...................................................................................................2-1

Using variables............................................................................................................2-2

Keywords.................................................................................................................... 2-3

Builtin functions..........................................................................................................2-4

Variable typing............................................................................................................ 2-5

Strings......................................................................................................................... 2-6

Single-delimited string literals.................................................................................... 2-7

Triple-delimited string literals.....................................................................................2-8

Raw string literals....................................................................................................... 2-9

Unicode characters....................................................................................................2-10

String operators and methods....................................................................................2-12

Numeric literals.........................................................................................................2-16

Math operators and expressions................................................................................2-18

Converting among types........................................................................................... 2-21

Writing to the screen................................................................................................. 2-22

String formatting....................................................................................................... 2-24

Command line parameters........................................................................................ 2-26

Reading from the keyboard.......................................................................................2-27

Chapter 3: Flow Control

Chapter 3 Objectives...................................................................................................3-1

About flow control...................................................................................................... 4-2

What's with the white space?...................................................................................... 4-3

if and elif..................................................................................................................... 4-4

Conditional expressions.............................................................................................. 4-5

Relational operators.................................................................................................... 4-6

Boolean operators....................................................................................................... 4-7

while loops..................................................................................................................4-9

Alternate ways to exit a loop.....................................................................................4-10

TOC-2 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 6: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Table of ContentsChapter 4: Lists and tuples

Chapter 4 Objectives...................................................................................................4-1

About sequences......................................................................................................... 4-2

Lists.............................................................................................................................4-3

Tuples..........................................................................................................................4-4

Indexing and slicing.................................................................................................... 4-5

Iterating through a sequence....................................................................................... 4-8

Using enumerate()................................................................................................... 4-10

Functions for all sequences....................................................................................... 4-12

Keywords and operators for all sequences................................................................4-14

The range() function.................................................................................................4-16

Nested sequences...................................................................................................... 4-18

List comprehensions................................................................................................. 4-20

Generator expressions............................................................................................... 4-23

Chapter 5: Working with files

Chapter 5 Objectives...................................................................................................5-1

Text file I/O................................................................................................................. 5-2

Opening a text file....................................................................................................... 5-3

The with block.............................................................................................................5-4

Reading a text file....................................................................................................... 5-5

Writing to a text file.................................................................................................... 5-7

Non-delimited (raw) data............................................................................................ 5-9

Chapter 6: Dictionaries and Sets

Chapter 6 Objectives...................................................................................................6-1

About dictionaries....................................................................................................... 6-2

When to use dictionaries............................................................................................. 6-3

Creating dictionaries................................................................................................... 6-4

Getting dictionary values............................................................................................ 6-7

Iterating through a dictionary......................................................................................6-8

Reading file data into a dictionary............................................................................ 6-10

Counting with a dictionary........................................................................................6-12

About sets..................................................................................................................6-13

Creating sets..............................................................................................................6-14

Working with sets......................................................................................................6-16

Python 3 for Scientists TOC-3© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 7: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Table of Contents rev. 1.0

Chapter 7: Functions

Chapter 7 Objectives...................................................................................................7-1

Defining a function......................................................................................................7-2

Function parameters....................................................................................................7-3

Returning values......................................................................................................... 7-5

Variable scope............................................................................................................. 7-6

Chapter 8: Errors and Exception Handling

Chapter 8 Objectives...................................................................................................8-1

Syntax errors............................................................................................................... 8-2

Exceptions...................................................................................................................8-3

Handling exceptions with try..................................................................................... 8-4

Handling multiple exceptions..................................................................................... 8-5

Handling all exceptions...............................................................................................8-6

Ignoring exceptions.....................................................................................................8-7

Using else....................................................................................................................8-8

Cleaning up with finally............................................................................................. 8-9

The standard exception hierarchy............................................................................. 8-10

Chapter 9: Using the Standard Library

Chapter 9 Objectives...................................................................................................9-1

The sys module........................................................................................................... 9-2

Interpreter information................................................................................................9-3

STDIO.........................................................................................................................9-4

Launching external programs......................................................................................9-5

Paths, directories, and filenames................................................................................. 9-8

Walking directory trees............................................................................................. 9-12

Grabbing web pages..................................................................................................9-15

Sending e-mail.......................................................................................................... 9-16

Math functions.......................................................................................................... 9-18

Random values..........................................................................................................9-19

Dates and times......................................................................................................... 9-21

Zipped archives......................................................................................................... 9-23

TOC-4 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 8: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Table of ContentsChapter 10: Pythonic Programming

Chapter 10 Objectives...............................................................................................10-1

The Zen of Python.....................................................................................................10-2

Common Python idioms............................................................................................10-3

Slicing and dicing......................................................................................................10-4

Unpacking function arguments................................................................................. 10-6

Iterable unpacking.....................................................................................................10-7

Extended Iterable unpacking.....................................................................................10-9

Lambda functions....................................................................................................10-11

List comprehensions............................................................................................... 10-12

Iterables...................................................................................................................10-13

Generator expressions............................................................................................. 10-14

Generator functions.................................................................................................10-16

Dictionary comprehensions.....................................................................................10-17

io.StringIO...............................................................................................................10-18

Long strings.............................................................................................................10-19

String formatting..................................................................................................... 10-20

Chapter 11: Modules and packages

Chapter 11 Objectives............................................................................................... 11-1

What is a module?.....................................................................................................11-2

The import statement................................................................................................11-3

Where did that .pyc file come from?.........................................................................11-4

Module search path................................................................................................... 11-5

Creating Modules...................................................................................................... 11-6

Packages..................................................................................................................11-10

Module aliases.........................................................................................................11-11

When the batteries aren't included.......................................................................... 11-12

Python 3 for Scientists TOC-5© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 9: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Table of Contents rev. 1.0

Chapter 12 – Classes

Chapter 12 Objectives...............................................................................................12-1

Defining classes.........................................................................................................12-2

Instance objects......................................................................................................... 12-3

Instance attributes..................................................................................................... 12-4

Instance Methods...................................................................................................... 12-5

__init__..................................................................................................................... 12-6

Properties.................................................................................................................. 12-7

Class data.................................................................................................................. 12-9

Class methods......................................................................................................... 12-10

Inheritance...............................................................................................................12-11

Multiple inheritance................................................................................................ 12-13

Using super()...........................................................................................................12-14

Special methods...................................................................................................... 12-15

Class-private variables............................................................................................ 12-21

Static methods......................................................................................................... 12-22

Chapter 13: Developer Tools

Chapter 13 Objectives...............................................................................................13-1

Program development............................................................................................... 13-2

Comments................................................................................................................. 13-3

pylint.........................................................................................................................13-4

Customizing pylint................................................................................................... 13-5

Using pyreverse........................................................................................................13-6

The unittest module..................................................................................................13-7

Fixtures......................................................................................................................13-9

Skipping tests.......................................................................................................... 13-11

Making a suite of tests............................................................................................ 13-12

Automated test discovery........................................................................................13-14

The Python debugger.............................................................................................. 13-15

Starting debug mode............................................................................................... 13-16

Stepping through a program....................................................................................13-17

Setting breakpoints..................................................................................................13-18

Profiling...................................................................................................................13-19

Benchmarking......................................................................................................... 13-20

TOC-6 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 10: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Table of ContentsChapter 14 – Serializing Data

Chapter 14 Objectives...............................................................................................14-1

About XML...............................................................................................................14-2

Normal approaches to XML..................................................................................... 14-3

Which module to use?...............................................................................................14-4

Getting started with ElementTree............................................................................. 14-5

How ElementTree works...........................................................................................14-6

Creating a new XML Document............................................................................... 14-8

Parsing an XML Document...................................................................................... 14-1

Navigating the XML Document................................................................................14-2

Using XPath.............................................................................................................. 14-5

About JSON.............................................................................................................. 14-9

Reading JSON.........................................................................................................14-20

Writing JSON..........................................................................................................14-21

Reading CSV data................................................................................................... 14-23

Nonstandard CSV................................................................................................... 14-24

Writing CSV data.................................................................................................... 14-26

Chapter 15 – Excel Spreadsheets

Chapter 15 Objectives...............................................................................................15-1

The openpyxl module................................................................................................15-2

Reading an existing spreadsheet............................................................................... 15-3

Working with ranges................................................................................................. 15-5

Worksheet info.......................................................................................................... 15-6

Modifying a worksheet............................................................................................. 15-7

Working with formulae............................................................................................. 15-8

Creating a new spreadsheet.....................................................................................15-10

Setting styles........................................................................................................... 15-11

Python 3 for Scientists TOC-7© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 11: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Table of Contents rev. 1.0

Chapter 16: iPython and Jupyter

Chapter 16 Objectives...............................................................................................16-1

About iPython........................................................................................................... 16-2

Features of iPython................................................................................................... 16-3

The many faces of iPython........................................................................................16-4

Starting iPython.........................................................................................................16-5

Getting help...............................................................................................................16-6

Tab completion..........................................................................................................16-7

Magic commands...................................................................................................... 16-8

Benchmarking........................................................................................................... 16-9

External commands.................................................................................................16-10

Enhanced help......................................................................................................... 16-11

Jupyter notebooks................................................................................................... 16-12

Cells........................................................................................................................ 16-13

Sharing notebooks...................................................................................................16-14

Chapter 17: numpy

Chapter 17 Objectives...............................................................................................17-1

Python's scientific stack............................................................................................ 17-2

numpy overview........................................................................................................17-3

Creating arrays.......................................................................................................... 17-4

Creating ranges......................................................................................................... 17-7

Working with arrays..................................................................................................17-9

Shapes..................................................................................................................... 17-12

Slicing and indexing................................................................................................17-15

Indexing with booleans........................................................................................... 17-17

Stacking...................................................................................................................17-19

Iterating................................................................................................................... 17-21

Array creation shortcuts.......................................................................................... 17-23

Matrices...................................................................................................................17-26

Data types................................................................................................................17-28

Numpy Example List with Doc.............................................................................. 17-29

TOC-8 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 12: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Table of ContentsChapter 18: scipy

Chapter 18 Objectives...............................................................................................18-1

About scipy............................................................................................................... 18-2

Polynomials...............................................................................................................18-3

Vectorizing functions................................................................................................ 18-6

Doing real work........................................................................................................ 18-7

SciPy subpackages.................................................................................................... 18-8

Getting help...............................................................................................................18-9

Python and C/C++...................................................................................................18-10

SciPy subpackages.................................................................................................. 18-11

Chapter 19: pandas

Chapter 19 Objectives...............................................................................................19-1

About pandas........................................................................................................... 19-2

Pandas architecture................................................................................................... 19-3

Series.........................................................................................................................19-4

DataFrames............................................................................................................... 19-6

Data Alignment....................................................................................................... 19-10

Index Objects.......................................................................................................... 19-12

Basic Indexing.........................................................................................................19-15

Broadcasting............................................................................................................19-18

Removing entries.................................................................................................... 19-21

Time series.............................................................................................................. 19-23

Reading Data...........................................................................................................19-27

Chapter 20: matplotlib

Chapter 20 Objectives...............................................................................................20-1

About matplotlib....................................................................................................... 20-2

matplotlib architecture.............................................................................................. 20-3

matplotlib Terminology.............................................................................................20-4

matplotlib keeps state................................................................................................20-5

What else can you do?.............................................................................................. 20-6

Python 3 for Scientists TOC-9© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 13: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Table of Contents rev. 1.0

Chapter 21: Pillow

Chapter 21 Objectives...............................................................................................21-1

Pillow........................................................................................................................ 21-2

Supported image file types........................................................................................21-3

The Image class.........................................................................................................21-4

Reading and writing.................................................................................................. 21-5

Creating thumbnails.................................................................................................. 21-6

Coordinate system.....................................................................................................21-7

Cropping and pasting................................................................................................ 21-8

Rotating, resizing, and flipping................................................................................. 21-9

Enhancing................................................................................................................21-11

Appendix A: Bibliography

Appendix B: Python Gotchas

Appendix C: String Formatting

TOC-10 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 14: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Python 3 for Scientists

About this CourseEVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 15: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 16: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 About this Course

Welcome!

● We're glad you're here

● Class has hands-on labs for nearly every chapter

● Please make a name tent

● Instructor name: ____________________________

● Instructor e-mail: ___________________________

Have Fun!

Python 3 for Scientists ATC-1© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 17: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

About this course rev. 1.0

Classroom etiquette

● Noisemakers off

● No phone conversations

● Come and go quietly during class.

Please turn off cell phone ringers and other noisemakers.

If you need to have a phone conversation, please leave the classroom.

We're adults here; feel free to leave the clasroom if you need to use the restroom, make a phone call, etc. You don't have to wait for a lab or break, but please try not to disturb your fellow students.

Please do not bring killer rabbits to class. They might maim, dismember, or otherwise disturb your fellow students.

ATC-2 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 18: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 About this Course

Course Outline

Day 1Chapter 1 The Python Environment

Chapter 2 Getting Started

Chapter 3 Flow Control

Chapter 4 Lists and Tuples

Chapter 5 Working with files

Day 2Chapter 6 Dictionaries and sets

Chapter 7 Functions

Chapter 8 Exceptions

Chapter 9 Using the standard library

Day 3Chapter 10 Pythonic Programming

Chapter 11 Modules and Packages

Chapter 12 Classes

Chapter 13 Developer Tools

Day 4Chapter 14 Serializing Data

Chapter 15 Working with Excel

Chapter 16 iPython and Jupyter

Chapter 17 numpy

Day 5Chapter 18 scipy

Chapter 19 pandas

Chapter 20 matplotlib

Chapter 21 The Python Imaging Library

The actual schedule may vary

Python 3 for Scientists ATC-3© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 19: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

About this course rev. 1.0

Student filesYou will need to load some student files onto your computer. The files are in a compressed archive. When you extract them onto your computer, they will all be extracted into a directory named py3forsci.

What's in the files?

py3forsci contains data and other files needed for the exercises

py3forsci/EXAMPLES contains the examples from the course manuals.

py3forsci/ANSWERS contains sample answers to the labs.

py3forsci/py3forsci_links.html contains links relevant to the course.

The student files do not contain Python itself. It will need to be installed separately. This has probably already been done for you.

ATC-4 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 20: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 About this Course

Extracting the student files

Windows

Open the file py3forsci.zip. Extract all files to your desktop (or any other convenient location). This will create the folder py3forsci.

Non-Windows (Linux, MacOS, etc)

Copy the file py3forsci.tgz to any convenient location. In your home directory, type

tar xzvf py3forsci.tgzThis will create the py3forsci directory under your home directory.

Python 3 for Scientists ATC-5© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 21: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

About this course rev. 1.0

Examples

● Most examples from course manual provided in EXAMPLES subdirectory

● First line of output shows how script was invoked

It will look like this:

Exampleunicode.py

print u"26\u00B0"print u"26\N{DEGREE SIGN}"print u"26\u00B0\n"print ur"26\u00B0\n"print ur"26\N{DEGREE SIGN}"

unicode.py26°26°26°

26°\n26\N{DEGREE SIGN}

ATC-6 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 22: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 About this Course

Lab Exercises

● Relax – the labs are not quizzes

● Feel free to modify labs

● Ask the instructor for help

● Work on your own scripts or data

● Answers are in py3forsci/ANSWERS

Python 3 for Scientists ATC-7© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 23: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

About this course rev. 1.0

Appendices

● Appendix A: Using SymPy

● Appendix B: Python Bibliography

● Appendix C: String formatting

ATC-8 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 24: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Python 3 for Scientists

Chapter 1

The Python Environment

"I think the real key to Python's platform independence is that it was conceived right from the start as only very loosely tied to Unix."

-- Guido van Rossum

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 25: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 26: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 The Python Environment

Chapter 1 Objectives

● Using the interpreter

● Getting help

● Running scripts on Windows, Unix, and MacOS

● Learning best editors and IDEs

Python 3 for Scientists 1-1© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 27: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

The Python Environment rev. 1.0

Starting Python

● Type python (possibly python3) at a command prompt

● Python should be in your PATH

● If python not found, install it or add directory to PATH

To start the Python 3 interpreter, just type python at the command prompt. If you get an error message, one of two things has happened:

1. Python 3 is not installed on your computer

2. The Python 3 interpreter is not in your PATH variable

If Python 3 is not installed, the solution is evident.

1-2 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 28: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 The Python Environment

If the interpreter is not in your PATHIf the directory where the interpreter lives is not in your PATH variable, you have several choices.

Type the full path

Start python by typing the full path to the interpreter (e.g. /usr/bin/python3)

Add the directory to PATH temporarily

For Unix and Unix-like OSes

PATH="$PATH:/usr/dev/bin" sh,ksh,bash

setenv PATH "$PATH:/usr/dev/bin" csh,tcsh

For Windows (at a command prompt)

set PATH="%PATH%";c:\python32

Add the directory to PATH permanently

For Unix and Unix-like OSes, put a line to add the directory containing the Pytyon interpreter to your PATH variable in your shell startup file (e.g. .profile). Ask your system administrator for help. The command should look something like

PATH="$PATH:/path/to/python"

For Windows, right-click on My Computer icon. Select Properties, and then select the Advanced tab. Click on the Environment Variables button, then double-click on "Path" in the System Variables area at the bottom of the dialog. Add the directory for the Python interpreter tothe existing value and click OK.

Python 3 for Scientists 1-3© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 29: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

The Python Environment rev. 1.0

Using the interpreter

● Type in any Python command

● Prompt is >>>

● Command line editing is supported

● Exit with Ctrl-D (Unix) or Ctrl-Z <Enter> (Windows)

Once you have started the Python interpreter, it provides a character based interactive interpreter.The prompt is ">>>". You can type in any Python commands at this prompt.

For Unix, Python supports Gnu command line editing, which uses emacs-style commands. Thesecommands are detailed in the table on the next page.

For Windows, it supports the editing keys on a standard keyboard, which include Home, End, etc., as well as the arrow keys. Normal PC shortcuts such as Ctrl-RightArrow to jump to the nextword also work.

On all versions, you can use arrow keys and backspace to edit the line.

emacs-mode command line editingEmacs-mode Command

Function

^P Previous command^N Next command^F Forward 1 character^B Back 1 character^A Beginning of line^E End of line^D Delete character under cursor^K Delete to end of lineN/A Append text after cursorN/A Insert text before cursor

1-4 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 30: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 The Python Environment

Trying out a few commandsTry out the following commands in the interpreter:

print("Hello, world")print(4 + 3)print(10/3)print(10.0/3.0)You don't really need print().

"Hello, world"4 + 3

The interpreter automatically prints out the result of what you have typedin.

Python 3 for Scientists 1-5© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 31: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

The Python Environment rev. 1.0

The help() function

● Syntax>>>help(object)

>>>help("object")

The help() function provides immediate help from the Python documentation. If something has already been loaded, you can pass help() the object; otherwise, pass it the name of the object as astring.

Example>>>help(str)...>>>help("re")...

1-6 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 32: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 The Python Environment

Running a Python script

● Use python interpreter

● Any OS

To run a Python script stored in a file, merely call the Python interpreter with the script as its argument:

python myscript.py Windows

python myscript Unixor

python3 myscript Unix

This explicit execution will work on any operating system.

If you are sure that Python 3 is installed, and the above technique does not work, it might be because the python interpreter is not in your path. See the earlier discussion about running in interactive mode.

Python 3 for Scientists 1-7© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 33: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

The Python Environment rev. 1.0

Python scripts on Unix

● Put "shebang" line at top of script

● Add execute permission to file

● execute with script or ./script

To execute a Python script implicitly (without typing "python") under Unix, there are two steps. First, add a line (called the shebang) at the top of the file with the full path of the Python interpreter:

#!/usr/bin/pythonor

#!/usr/bin/python3Second, set the permission on the file to include execute, with a command such as

chmod 755 scriptThen, just type the name of the script. If the current directory is not included in the PATH variable, prefix the script name with ./.

Example

vi hello

chmod 755 hello

helloor

./hello

Modern versions of Unix, especially Linux, have an improved scheme forspecifying the interpreter for scripts. Instead of hard-coding the path to the interpreter, such as /usr/local/bin/python in the shebang line, this new mechanism uses the env utility, which takes a program name and finds it via the PATH variable. Thus,

#!/usr/bin/env python

will run the current script using the first python interpreter found, just as ifyou typed "python" on the command line. This makes your scripts more portable, as they do not depend on Python being in a hard-coded location.

1-8 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 34: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 The Python Environment

Python scripts on Windows

● Use extension .py

● Launch script from command line or file browser

Running a Python script implicitly on Windows is simple. Give the script the extension .py, and type its name (or run it from a file browser).

The script doesn't need any special code, nor does it need any special permissions.

Unfortunately, the Windows install of Python frequently does not set up the PATH environment variable correctly. To fix this, you need to add some paths to PATH.

To set the PATH variable, go to Computer->Properties->Advanced->Environment Variables

Add the following folders to the PATH variable, as shown. (If you are using a different version of Python, or it is installed in a different place, modify the following as needed). C:\python34;C:\python34\lib;C:\python34\scripts;C:\python34\tools\scripts;existing-path

Once this is done, close any open CMD windows and open a new one. You should now be able to run pydoc from the command prompt.

Python 3 for Scientists 1-9© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 35: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

The Python Environment rev. 1.0

Python Editors and IDEs

● Editor is programmer's most-used tool

● Select Python-aware editor or IDE

● Many open source and commercial choices

There are two pages on the Python Wiki that discuss editors and IDEs:

http://wiki.python.org/moin/PythonEditorshttp://wiki.python.org/moin/IntegratedDevelopmentEnvironments

Multi-platform IDEs include PyCharm Community Edition, Eclipse (open source) and Komodo Edit. These work on Windows, Unix/Linux, and Mac platforms.

1-10 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 36: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 The Python Environment

Chapter 1 Exercises

Exercise 1-1 (hello.py)

Write a "Hello, world" python script.

Run hello.py explicitly, from the command line

Run hello.py implicitly, from the command line

Run hello.py via the IDE.

Python 3 for Scientists 1-11© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 37: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

The Python Environment rev. 1.0

1-12 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 38: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Python 3 for Scientists

Chapter 12

ClassesEVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 39: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 40: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Chapter 12 Objectives

● Understanding the big picture of OO programming

● Defining a class and its constructor

● Creating object methods

● Adding attributes and properties to a class

● Adding class data and methods

● Using inheritance for code reuse

● Knowing when to use classes

Python 3 for Scientists 12-1© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 41: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Defining Classes

● Syntaxclass classname(base_class,...):

# class body – methods and data

● Classes create a namespace

The class statement defines a class and assigns it to a name.

The simplest form of class definition looks like this:

class ClassName(object): passNormally, the contents of a class definition will be method definitions and shared data.

A class definition creates a new local namespace. All variable assignments go into this new namespace. All methods are called via the class name.

A list of base classes is specified in parentheses after the class name. Always specify at least one base class. If you do not need to inherit from a specific class, use the default base class object.

Class definitions that do not specify a base class are legal, but discouraged. They create what are known as "old-style classes".

12-2 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 42: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Instance Objects

● Call class name as a function

● self contains attributes: properties & methods

● Syntaxobj = Class(args)

An object instance is an object created from a class. Each object instance has its own private attributes, which were perhaps initialized in the __init__ method.

Python 3 for Scientists 12-3© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 43: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Instance attributes

● Methods and data

● Accessed using dot notation

● Privacy by convention (_name)

An instance of a class (AKA object) normally contains methods and data. To access these attributes, use "dot notation": object.attribute.

Instance attributes are dynamic; they can be accessed directly from the object. You can create, update, and delete attributes in this way.

Attributes cannot be made private, but names that begin with an underscore are understood by convention to be for internal use only. Users of your class will not consider methods that begin with an underscore to be part of your class's API. The from module import * idiom does not import attributes that begin with an underscore.

Exampleclass Spam(object): def eggs(self): pass

def _beverage(self): # private! pass

s = Spam()s.eggs()s.toast = 'buttered'print(s.toast)

s._beverage() # legal, but wrong!

In most cases, it is better to use properties (described later) to access data attributes.

12-4 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 44: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Instance Methods

● Called from objects

● Passed invoking objects as implicit parameter

A method is a function defined in a class. When a method is called from an object, the object is passed in as the implicit first parameter, named self by strong convention.

Examplerabbit.py

class Rabbit:

def __init__(self,size,danger): self._size = size self._danger = danger self._victims = []

def threaten(self): print("I am a {0} bunny with {1}!".format( self._size, self._danger ) )

r1 = Rabbit('large',"sharp, pointy teeth") r1.threaten()

r2 = Rabbit('small','fluffy fur') r2.threaten()

rabbit.pyI am a large bunny with sharp, pointy teeth! I am a small bunny with fluffy fur!

In C++, Java, and C#, self might be called this.

Python 3 for Scientists 12-5© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 45: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

__init__

● Constructor

● Implicitly called when object is created

If a class defines a method named __init__ , it will be automatically called when an object instance is created. This is the constructor.

The object being created is implicitly passed as the first parameter to __init__ . This parameter isnamed self by very strong convention. Data attributes can be assigned to self. These attributes can then be accessed by other methods.

Exampleclass Rabbit:

def __init__(self,size,danger): self._size = size self._danger = danger self._victims = []

12-6 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 46: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Properties

● Accessed like attributes

● Invoke implicit getters, setters, and deleters

While object attributes can be accessed directly, in many cases the class needs to exercise some control over the attributes.

One approach is to provide getter, setter, and deleter methods for attributes. These are methods that can be called to indirectly manage attributes. There are many variations on the implementation of these methods.

Example

class Knight(object): def __init__(self,name): self._name = name

def set_name(self,name): self._name = name

def get_name(self): return self._name

k = Knight("Lancelot")print( k.get_name() )

A more elegant approach is to use properties. A property is sometimes called a managed attribute. Properties are accessed directly, like normal attributes, but getter, setter, and deleter functions are implicitly called, so that the class can control what values are stored or retrieved from the attributes.

Python 3 for Scientists 12-7© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 47: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Exampleknight_properties.py

class Knight(object): def __init__(self,name): self._name = name

@property def name(self): return self._name

@name.setter def name(self, name): self._name = name

@property def color(self): return self._color

@color.setter def color(self, color): self._color = color

k = Knight("Lancelot")k.color = 'blue'

# Bridgekeeper's questionprint('Sir {0}, what is your...favorite color?'.format( k.name ))# Knight's answerprint("red, no -- {0}!".format( k.color ))

knight_properties.pySir Lancelot, what is your...favorite color? red, no – blue!

12-8 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 48: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Class Data

● Attached to class, not instance

● Shared by all instances

Data can be attached to the class itself, and shared among all instances. Class data can be accessed via the class name from inside or outside of the class.

Any class attribute not overwritten by an instance attribute is also available through the instance.

Exampleclass_data.pyclass Rabbit: weapon = 'a nice cup of tea' name = 'Roger' def __init__(self): self.weapon = 'pointy teeth' def display(self): print("weapon -- CLASS: {0} INSTANCE: {1}".format(Rabbit.weapon,self.weapon)) print("name -- CLASS: {0} INSTANCE: {1}".format(Rabbit.name,self.name))

r = Rabbit() r.display()

class_data.pyweapon -- CLASS: a nice cup of tea INSTANCE: pointy teeth name -- CLASS: Roger INSTANCE: Roger

Python 3 for Scientists 12-9© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 49: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Class Methods

● Called from class or instance

● Use @classmethod to define

● First parameter named "cls" by convention

If a method only needs class attributes, it can be made a class method via the @classmethod decorator. This alters the method so that it gets a copy of the class object rather than the instanceobject. This is true whether the method is called from the class or from an instance.

The parameter to a class method is named cls by strong convention.

Exampleclassmethod.py

class Rabbit: weapon = 'pointy teeth'

def get_weapon(self): print("INSTANCE: ({0}) {1}".format(self,Rabbit.weapon))

@classmethod def get_weapon_again(cls): print("CLASS: ({0}) {1}".format(cls,cls.weapon))

r = Rabbit()

r.get_weapon() r.get_weapon_again() Rabbit.get_weapon_again() print(Rabbit.weapon)

classmethod.pyINSTANCE: (<__main__.Rabbit instance at 0xb77dc52c>) pointy teeth CLASS: (__main__.Rabbit) pointy teeth CLASS: (__main__.Rabbit) pointy teeth pointy teeth

12-10 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 50: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Inheritance

● Specify base class in class definition

● Call base class constructor explicitly

Of course, a language feature would not be worthy of the name "class"' without supporting inheritance. One or more base classes may be specified as part of the class definition. All of the previous examples in this chapter have used the default base class, object.

The base class must already be imported, if necessary. If a requested attribute is not found in the class, the search looks in the base class. This rule is applied recursively if the base class itself is derived from some other class.

Classes may override methods of their base classes. (For Java and C++ programmers: all methods in Python are effectively virtual.)

To extend rather than simply replace a base class method, call the base class method directly: BaseClassName.methodname(self, arguments).

You can also use the super() function, which stands in for the base class:

class Foo(Bar): def __init__(self): super().__init__() # same as Bar._init__(self)

The advantage of super() is that you don't have to specify the base class explicitly, so if you change the base class, it automatically does the right thing.

The abc module in the standard library provides a way to create abstractbase classes

Python 3 for Scientists 12-11© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 51: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Exampleanimalvan.py

class AnimalVan(object):

def __init__(self,target,action): self._target = target self._action = action

def act(self): print("Squad! Eyes front! Stand at ease. {0} {1}{2} ...{1}!".format( self._target.capitalize(), self._action, self._get_plural_ending(), ))

def _get_plural_ending(self): ''' Get correct plural ending ''' if self._action.endswith("e"): suffix = "rs" elif self._action[-1] in "bdfglmnprst": suffix = self._action[-1] + "ers" else: suffix = "ers" return suffix stoatvan.py

from animalvan import AnimalVan

class StunAStoat(AnimalVan): def __init__(self): super().__init__('stoat','stun') volevan.py

from animalvan import AnimalVan

class AmazeAVole(AnimalVan): def __init__(self): super().__init__('vole','amaze') inheritance_ex.py

from animalvan import AnimalVan from stoatvan import StunAStoat from volevan import AmazeAVole

catvan = AnimalVan('cat','confuse') catvan.act()

stoatvan = StunAStoat() stoatvan.act()

volevan = AmazeAVole() volevan.act()

inheritance_ex.pySquad! Eyes front! Stand at ease. Cat confusers ...confuse! Squad! Eyes front! Stand at ease. Stoat stunners ...stun! Squad! Eyes front! Stand at ease. Vole amazers ...amaze!

12-12 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 52: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Multiple Inheritance

● More than one base class

● All data and methods are inherited

● Methods resolved left-to-right, depth-first

Python classes can inherit from more than one base class. This is called "multiple inheritance". Aclass definition with multiple base classes looks like this:

class DerivedClassName(Base1, Base2, Base3): <statement-1> . . . <statement-N>The method search algorithm linearizes the search order in a way that preserves the left-to-right ordering specified in each class, that calls each parent only once, and that is monotonic (meaning that a class can be subclassed without affecting the precedence order of its parents).

For more information on method resolution order (MRO), see http://www.python.org/download/releases/2.3/mro/.

Python 3 for Scientists 12-13© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 53: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Using Super

● Follows MRO to find function

● OK for single inheritance tree

● Use explicit base class names for multiple inheritance

● Syntax: super().method()

The super() function can be used in a class to invoke methods in base classes. It searches the base classes and their bases, recursively, from left to right until the method is found.

For classes that have a single inheritance tree, this works great. For classes that have a diamond-shaped tree, super() may not do what you expect. In this case, using the explicit base class name is best.

Exampleinsect.py

from animal import Animal

class Insect(Animal): ''' An animal with 2 sets of wings and 3 pairs of legs ''' def __init__(self, species, name, sound, can_fly=True): super().__init__(species, name, sound) self._can_fly = can_fly @property def CanFly(self): return self._can_fly if __name__ == '__main__': mon = Insect('monarch butterfly', 'Mary', None) scar = Insect('scarab beetle', 'Rupert', 'Bzzz', False) for insect in mon, scar: flying_status = 'can' if insect.CanFly else "can't" print("Hi! I am {0} the {1} and I {2} fly!".format( insect.Name, insect.Species, flying_status ), ) insect.MakeSound() print()

insect.pyHi! I am Mary the monarch butterfly and I can fly! None

Hi! I am Rupert the scarab beetle and I can't fly! Bzzz

12-14 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 54: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Special Methods

● User-defined classes emulate standard types

● Define behavior for builtin functions

● Override operators

Python has a set of special methods that can be used to make user-defined classes emulate the behavior of builtin classes. These methods can be used to define the behavior for builtin functions such as str(), len() and repr(); they can also be used to override many Python operators, such as +, *, and ==.

These methods expect the self parameter, like all instance methods. They frequently take one or more additional methods. self. Is the object being called from the builtin function, or the left operand of a binary operator such as ==.

For instance, if your object represented a database connection, you could have str() return the hostname, port, and maybe the connection string. The default for str() is to call repr(), which returns something like <__main__.DBConn object at 0xb7828c6c>, which is not nearly so user-friendly.

See http://docs.python.org/reference/datamodel.html#special-method-names for detailed documentation on the special methods.

Python 3 for Scientists 12-15© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 55: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Special Methods and VariablesMethod or Variables Description

__new__(cls,...) Returns new object instance; Called before __init__()

__init__(self,...) Object initializer (constructor)

__del__(self) Called when object is about to be destroyed

__repr__(self) Called by repr() builtin

__str__(self) Called by str() builtin

__eq__(self, other)

__ne__(self, other)

__gt__(self, other)

__lt__(self, other)

__ge__(self, other)

__le__(self, other)

Implement comparison operators ==, !=, >, <, >=, and <=. self is object on the left.

__cmp__(self, other) Called by comparison operators if __eq__, etc., are not defined

__hash__(self) Called by hash() builtin, also used by dict, set, and frozenset operations

__nonzero__(self) Called by bool() builtin. Implements truth value (boolean) testing. If notpresent, bool() uses len()

__unicode__(self) Called by unicode() builtin

__getattr__(self, name)

__setattr__(self, name, value)

__delattr__(self, name)

Override normal fetch, store, and deleter

__getattribute__(self, name) Implement attribute access for new-style classes

__get__(self, instance)

__set__(self, instance, value)

__del__(self, instance)

Implement descriptors

__slots__ = variable-list Allocate space for a fixed number of attributes.

__metaclass__ = callable Called instead of type() when class is created.

__instancecheck__(self, instance) Return true if instance is an instance of class

__subclasscheck__(self, instance) Return true if instance is a subclass of class

__call__(self, ...) Called when instance is called as a function.

__len__(self) Called by len() builtin

__getitem__(self, key) Implements self[key]

__setitem__(self, key, value) Implements self[key] = value

__selitem__(self, key) Implements del self[key]

__iter__(self) Called when iterator is applied to container

__reversed__(self) Called by reversed() builtin

__contains__(self, object) Implements in operator

12-16 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 56: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Special Methods and VariablesMethod or Variables Description

__add__(self, other)

__sub__(self, other)

__mul__(self, other)

__floordiv__(self, other)

__mod__(self, other)

__divmod__(self, other)

__pow__(self, other[, modulo])

__lshift__(self, other)

__rshift__(self, other)

__and__(self, other)

__xor__(self, other)

__or__(self, other)

Implement binary arithmetic operators +, -, *, //, %, **, <<, >>, &, ^, and |. Self is object on left side of expression.

__div__(self,other)__truediv__(self,other)

Implement binary division operator /. __truediv__ is called if __future__.division is in effect.

__radd__(self, other)

__rsub__(self, other)

__rmul__(self, other)

__rdiv__(self, other)

__rtruediv__(self, other)

__rfloordiv__(self, other)

__rmod__(self, other)

__rdivmod__(self, other)

__rpow__(self, other)

__rlshift__(self, other)

__rrshift__(self, other)

__rand__(self, other)

__rxor__(self, other)

__ror__(self, other)

Implement binary arithmetic operators with swapped operands. (Used ifleft operand does not support the corresponding operation)

Python 3 for Scientists 12-17© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 57: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Special Methods and VariablesMethod or Variables Description

__iadd__(self, other)

__isub__(self, other)

__imul__(self, other)

__idiv__(self, other)

__itruediv__(self, other)

__ifloordiv__(self, other)

__imod__(self, other)

__ipow__(self, other[, modulo])

__ilshift__(self, other)

__irshift__(self, other)

__iand__(self, other)

__ixor__(self, other)

__ior__(self, other)

Implement augmented (+=, -=, etc.) arithmetic operators.

__neg__(self)

__pos__(self)

__abs__(self)

__invert__(self)

Implement unary arithmetic operators -, +, abs(), and ~.

__oct__(self)

__hex__(self)

Implement oct() and hex() builtins

__index__(self) Implement operator.index()

__coerce__(self, other) Implement "mixed-mode" numeric arithmetic.

12-18 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 58: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 ClassesExamplespecialmethods.py

class Special(object): def __init__(self,value): self._value = str(value) # what happens when Special object added to another Special object def __add__(self,other): return self._value + other._value # what happens when a Special object is multiplied by a value def __mul__(self,num): return ''.join((self._value for i in range(num))) # define what happens when str() called on a Special object def __str__(self): return self._value.upper() def __eq__(self,other): return self._value == other._value if __name__ == '__main__': s = Special('spam') t = Special('eggs') u = Special('spam') v = Special(5) w = Special(22) print("s + s", s + s) print("s + t", s + t) print("t + t", t + t) print("s * 10", s * 10) print("t * 3", t * 3) print("str(s)={0} str(t)={1}".format( str(s), str(t) )) print("id(s)={0} id(t)={1} id(u)={2}".format(id(s), id(t), id(u) )) print("s == s", s == s) print("s == t", s == t) print("u == s", s == u) print("v + v", v + v) print("v + w", v + w) print("w + w", w + w) print("v * 10", v * 10) print("w * 3", w * 3)

Python 3 for Scientists 12-19© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 59: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

specialmethods.pys + s spamspam s + t spameggs t + t eggseggs s * 10 spamspamspamspamspamspamspamspamspamspam t * 3 eggseggseggs str(s)=SPAM str(t)=EGGS id(s)=3069862348 id(t)=3069862412 id(u)=3069862444 s == s True s == t False u == s True v + v 55 v + w 522 w + w 2222 v * 10 5555555555 w * 3 222222

12-20 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 60: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Class-Private Variables

● Provide variables private to derived classes

● Start with 2 underscores

● At most 1 trailing underscore

Python supports class-private variables in a hackish way. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.

This mangling is done for all identifiers in a class. Outside classes, or when the class name consists of only underscores, no mangling occurs.

Thus, a base and derived class can both have the attribute __customer_num without one overwriting the other.

These mangling rules are designed mostly to avoid accidents; it still is possible for a determined programmer to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger, and that's one reason why this loophole is not closed.

Truncation may occur when the mangled name would be longer than 255 characters.

Python 3 for Scientists 12-21© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 61: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

Static Methods

● Related to class, but doesn't need instance or class object

● Use @staticmethod decorator

A static method is a utility method that is functionally related to the class, but does not need the instance or class object. Thus, it has no automatic parameter. Static methods are seldom needed.

One use case for static methods is to factor some kind of logic out of several methods, when the logic doesn't require any of the data in the class.

12-22 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 62: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

rev. 1.0 Classes

Chapter 12 Exercises

Exercise 12-1 (president.py)

Create a module named president that implements a President class. This class has a constructor that takes the index number of the president (1-44) and creates an object containing the associated information from the presidents.txt file.

Provide the following properties (types indicated after -->):

term_number --> int

first_name --> string

last_name --> string

birth_date --> date object

death_date --> date object (or None, if still alive)

birth_place --> string

birth_state --> string

term_start_date --> date object

term_end_date --> date object (or None, if still in office)

party --> string

Exercise 12-2 (president_test.py)

Write a test script to exercise all of the properties. It could look something like

from president import President

p = President(1) # George Washington

print("George was born at {0}, {1} on {2}".format( p.birth_place, p.birth_state, p.birth_date)

Python 3 for Scientists 12-23© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 63: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

Classes rev. 1.0

12-24 Python 3 for Scientists© 2016 CJ Associates

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied

Page 64: Unauthorized EVALUATION - ITCourseware...2018/07/24  · Table of Contents rev. 1.0 Chapter 7: Functions Chapter 7 Objectives.....7-1 Defining a

7400 E. Orchard Road, Suite 1450 NGreenwood Village, Colorado 80111

Ph: 303-302-5280www.ITCourseware.com

9-35-00045-000-07-24-18

EVALUATION COPY

Unauthorized reproduction or distribution is prohibitied