Guido van Rossum 9th LASER summer school, Sept. 2012

Similar documents
Iterators & Generators

Guido van Rossum 9th LASER summer school, Sept. 2012

Guido van Rossum 9th LASER summer school, Sept. 2012

Python 3000 and You. Guido van Rossum PyCon March 14, 2008

CS 331 Midterm Exam 1

CS 331/401 Summer 2018 Midterm Exam

Python 3000 and You. Guido van Rossum EuroPython July 7, 2008

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013

CSI33 Data Structures

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Python itertools. Stéphane Vialette. October 22, LIGM, Université Paris-Est Marne-la-Vallée

Iterators and Generators

Table of Contents EVALUATION COPY

Iterators and Generators

Python: Functions and Generators. Giuseppe Attardi

Guido van Rossum 9th LASER summer school, Sept. 2012

A Brief Introduction to Python for those who know Java. (Last extensive revision: Daniel Moroz, fall 2015)

What's New in Python 2.2

Introduction to Problem Solving and Programming in Python.

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

G Programming Languages - Fall 2012

Programming Paradigms

ITERATORS AND GENERATORS 10

Lecture 7: Python s Built-in. in Types and Basic Statements

ITERATORS AND STREAMS 9

Lecture 17A: Finite and Infinite Iterators

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Babu Madhav Institute of Information Technology, UTU 2015

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Asynchronous I/O: A Case Study in Python

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Data structure and algorithm in Python

A Brief Introduction to Python for those who know Java Last extensive revision: Jie Gao, Fall 2018 Previous revisions: Daniel Moroz, Fall 2015

CIS192 Python Programming

CIS192 Python Programming

Python-2. None. Special constant that is a null value

21 Years of Python. From Pet Project to Programming Language of the Year. Guido van Rossum May 2011

mith College Computer Science Week 12 CSC111 Spring 2018 Dominique Thiébaut

Computer Science 1001.py. Lecture 19, part B: Characters and Text Representation: Ascii and Unicode

Lecture no

Advanced Python. generators, decorators, context managers. Zbigniew Jędrzejewski-Szmek. George Mason University

Python - a Dynamic Programming Language. Guido van Rossum May 31, 2007

Python Evaluation Rules

CIS192 Python Programming

Advanced Python. Executive Summary, Session 1

Data Science with Python Course Catalog

Porting Python 2 Code to Python 3 Release 2.7.6

References and Mutable Data Structures

DSC 201: Data Analysis & Visualization

Data structure and algorithm in Python

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

Lists, loops and decisions

MUTABLE LISTS AND DICTIONARIES 4

First Programming Language in CS Education The Arguments for Scala

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

CIS192: Python Programming

A Tutorial Introduction. CS 3270 Chapter 1

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Sequences and iteration in Python

CSE 130, Fall 2005: Final Examination

PyTrie Documentation. Release 0.3. George Sakkis

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Python Crash-Course. C. Basso. Dipartimento di Informatica e Scienze dell Informazione Università di Genova. December 11, 2007

TEXT MINING INTRO TO PYTHON

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Modern Python Patterns and Idioms

CIS192 Python Programming

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

CSC326 Python Sequences i. CSC326 Python Sequences

Introduction to Python programming, II

Patterns The Essence of Functional Programming

Static Analysis of Dynamically Typed Languages made Easy

Python Review IPRE

Python and Bioinformatics. Pierre Parutto

Senthil Kumaran S

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

The Basil Project. (or What I took away from GRAD school ) Jonathan Riehl University of Chicago

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

solrq Documentation Release Michał Jaworski

Computer Science 1001.py. Lecture 19a: Generators continued; Characters and Text Representation: Ascii and Unicode

Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction

Intermediate Representation (IR)

Chapter 2 - Programming Language Syntax. September 20, 2017

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Python iterators and generators

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

CS Programming Languages: Python

Tree Oriented Programming. Jeroen Fokker

Sorting HOW TO Release 2.7.6

At full speed with Python

Python 3 10 years later

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

OOP and Scripting in Python Advanced Features

Extended Introduction to Computer Science CS1001.py Lecture 17: Cuckoo Hashing Finite and Infinite Iterators

Transcription:

Guido van Rossum guido@python.org 9th LASER summer school, Sept. 2012

It took many steps to get to Py3k generators Interesting example of a "random walk" Origins go as far back as it gets

ABC has datatypes text, list, table You can iterate over each of these FOR ch IN 'abc':... 'a', 'b', 'c' (order as specified in the string) FOR elem IN {'one'; 'two'; 'three'}:... 'one', 'three', 'two' (sorted) FOR val IN {1: 'one'; 3: 'three'; 2: 'two'}:... 'one', 'two', 'three' (values in key order) Note that the FOR-loop is polymorphic nearly the only thing strings, lists have in common

The for-loop works with strings, lists, tuples but not with dicts! These are unified into "sequences" Sequences support (among others): length: len(xs) indexing, slicing: xs[i], xs[i:j] concatenation, repetition: xs + ys, xs * n optionally: assignment to item or slice Type object has slots for each of these The for-loop calls len(xs) and xs[i] each time

Use case: a class wrapping e.g. a file so you can write e.g. for line in wrapped_file: print line Steve Majewski invented a hack: implement getitem () to get the "next" item implement len () to lie until exhausted Python 1.0.2 change: get xs[i] for increasing i until it raises IndexError avoids one (expensive) call per iteration implementing len () became optional

PEP 234 (Ka-Ping Yee, GvR, 2001) Separate "iterator" from "iterable" Downgrade "sequence" to backward compatibility All sequences become iterables Some new iterables added (e.g. dict) ironically, Python "for x in <dict>:" gives the keys The iterator object holds the iteration state E.g. list iteration: list object, index One iterable may have many iterators Iterator operation: it.next(), returns next value

How does an iterator indicate it is done? We had many long discussions about this Possibilities: separate has_next() or is_done() method return (value, done) tuple return special sentinel value when done raise exception when done In the end we chose the exception Plus a special case in the C code NULL without exception == exhausted

Now, "for x in xs: <body>" translates to: it = xs. iter () while True: try: x = it.next() except StopIteration: break <body> But we want to be able to pass an iterator! e.g. for x in xs. iter (): <body> Solution: iter(it) returns it Every iterator must support it.next(), iter(it)

PEP 255 (Neil Schemenauer, Tim Peters, M.L. Hetland, 2001) def foobar(n): for i in range(n): for j in range(i, n): yield j foobar(5) <generator object foobar at 0x104d93f00> list(foobar(5)) [0, 1, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4, 3, 4, 4] A generator is an iterator; not every iterator is a generator!

Umm... Parser recognizes 'yield' keyword (then forbids "return <expr>" syntax) Function gets flagged "is generator" (actually, it's the code object that gets flagged) When called, constructs generator object next(genobj) runs bytecode until next YIELD

PEP 342 (GvR, Phillip Eby, 2005) Change yield from statement to expression def f(): a = yield 'a' print(a) yield 'b' g = f() print(next(g)) print(g.send(1)) 'a' 1 'b'

Other APIs added: g.throw(exc) # Causes yield to raise exc g.close() # Complex protocol, GeneratorExit Allow yield inside try/finally Use cases: Coroutines exchanging values Async I/O using "trampoline" or scheduler Still not quite Knuth-style coroutines Can suspend only one frame Still can't use "return <expr>"

Python 2 protocol is it.next(), it. iter () PEP 234 explains why (with regret!): explicit calls expected considered prev(), current(), reset() as extensions Python 3 changed to it. next () PEP 3114 (Ka-Ping Yee, 2007) Also adds next(it) (We had iter(it) from the start) next(it) added in Python 2.6 to help transition

(Greg Ewing, 2009) Approximate semantics: Make "yield from <expr>" Equivalent to "for v in <expr>: yield v" Also allow "return <expr>" equivalent to "raise StopIteration(<expr>)" True semantics are more complex send() and throw() pass values down Use case: refactoring generators/coroutines Sadly, we have no experience using this yet

The itertools module has many useful ops: E.g. itertools.chain(it1, it2), itertools.islice(it, i, j) Can't we write these as overloaded operators? E.g. it1+it2, it[i:j] No! Iterators are a (nice, small) protocol There is no common base class We wouldn't want to burden each iterator implementation with such extra methods We could offer an optional base class but that would make use of the overloading unreliable