What's New in Python? "Not your usual list of new features" Stanford CSL Colloquium, October 29, 2003; BayPiggies, November 13, 2003 Guido van Rossum Elemental.

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Shared-Memory Model and Threads Intel Software College Introduction to Parallel Programming – Part 2.
Simplifications of Context-Free Grammars
4 Control Statements: Part 1.
Chapter 6 Writing a Program
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 2 Getting Started.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 Created by Cheryl M. Hughes, Harvard University Extension School Cambridge, MA The Web Wizards Guide.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Why I Invented Python EuroPython – June 27, 2005 Guido van Rossum Elemental Security, Inc.
Python: Building an Open Source Project and Community SDForum Distinguished Speaker Series, 2/17/05 Guido van Rossum Elemental Security,
Python Whats in a name? Snake logos and mascot notwithstanding, its named after Monty Pythons Flying Circus Humor-impaired can safely.
director of PythonLabs at Zope Corporation
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
10/09/1999© 1999 CNRI, Guido van Rossum 1 Computer Programming for Everybody Guido van Rossum CNRI (Corporation for National Research Initiatives, Reston,
AQute Eclipse Environment By Peter Kriens CEO aQute OSGi Director of Technology and OSGi Fellow.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
Chapter 6 File Systems 6.1 Files 6.2 Directories
Programming Language Concepts
Mike Scott University of Texas at Austin
Knowledge Extraction from Technical Documents Knowledge Extraction from Technical Documents *With first class-support for Feature Modeling Rehan Rauf,
Chapter 17 Recursion.
Turing Machines.
Red Tag Date 13/12/11 5S.
Database Performance Tuning and Query Optimization
Modern Programming Languages, 2nd ed.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
31242/32549 Advanced Internet Programming Advanced Java Programming
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Executional Architecture
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS 240 Computer Programming 1
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 15 Programming and Languages: Telling the Computer What to Do.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Pointers and Arrays Chapter 12
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Introduction to Recursion and Recursive Algorithms
Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
Languages for IT & CS Pseudo-code What HTML isn’t Early history Compiling & interpreting Classifying languages The process of programming.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
The Pumping Lemma for CFL’s
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
CS1022 Computer Programming & Principles
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Case Study 1: Iterators and Generators "Loops generalized and turned inside out"
About Python. Sept. 2003© Guido van Rossum 2 Executive Summary Dynamically typed object-oriented language Python programs look like executable.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
About Python.
What's New in Python? "Not your usual list of new features" Stanford CSL Colloquium, October 29, 2003; BayPiggies,
Presentation transcript:

What's New in Python? "Not your usual list of new features" Stanford CSL Colloquium, October 29, 2003; BayPiggies, November 13, 2003 Guido van Rossum Elemental Security

Sept. 2003© Guido van Rossum 2

Sept. 2003© Guido van Rossum 3 Talk Overview About me About Python Case study 1: iterators and generators Case study 2: new classes and descriptors Question Time

Sept. 2003© Guido van Rossum 4 About Me Age 4: first Lego kit Age 10: first electronics kit (with two transistors) Age 18: first computer program (on punched cards) Age 21: first girlfriend :-) 1982: "drs" math degree; joined CWI in Amsterdam 1987: first worldwide open source release 1989: started work on Python in spare time 1995: moved to Virginia, USA to join CNRI 2000: got married 2001: became a father 2003: moved to California to join Elemental Security

Sept. 2003© Guido van Rossum 5 About Elemental Security Enterprise security software Early stage startup in stealth mode Using lots of Python We're hiring! See

Sept. 2003© Guido van Rossum 6

About Python "The promotional package"

Sept. 2003© Guido van Rossum 8 Executive Summary Dynamically typed object-oriented language Python programs look like executable pseudo-code Supports multiple paradigms: –procedural, object-oriented, some functional Extensible in C, C++, Fortran,... Used by: –Google, ILM, NASA, Red Hat, RealNetworks,... Written in portable ANSI C (mostly...) Runs on: –Unix, Windows, Mac, Palm, VxWorks, PlayStation 2,... Jython: Java version, translates to Java byte code

Sept. 2003© Guido van Rossum 9 Why Use Python? Dynamic languages are more productive Python code is more readable Python code is more maintainable Python has fast built-in very high-level data types Developer time is more expensive than CPU time When Should You Not Use Python (Yet)? Things like packet filters, MP3 codecs, etc. Instead, write in C/C++ and wrap Python around it

Sept. 2003© Guido van Rossum 10 Example Function def gcd(a, b): "Greatest common divisor of two integers" while b != 0: a, b = b, a%b return a Note: –no declarations –indentation+colon for statement grouping –doc string part of function syntax –parallel assignment (to swap a and b: "a, b = b, a")

Sept. 2003© Guido van Rossum 11 Sample Use Areas Server-side web programming (CGI, app servers) Client-side web programming (HTML, HTTP,...) XML processing (including XML-RPC and SOAP) Databases (Oracle, MySQL, PostgreSQL, ODBC,...) GUI programming (Qt, GTK+, Tcl/Tk, wxPython,...) Scientific/numeric computing (e.g. LLNL) Testing (popular area for Jython) Scripting Unix and Windows Rapid prototyping (e.g. at Google) Programming education (e.g. Oxford physics) –from middle school to college

Sept. 2003© Guido van Rossum 12 Standard Library File I/O, socket I/O, web protocols (HTTP, CGI,...) XML, HTML parsing (DOM, SAX, Expat) Regular expressions (using standard Perl re syntax) compression (gzip/zlib, bz2), archiving (zip, tar) math, random, checksums, algorithms, data types date/time/calendar threads, signals, low-level system calls Python introspection, profiling, debugging, testing handling and much, much more! –and 10x more in 3rd party packages (e.g. databases)

Sept. 2003© Guido van Rossum 13 Python Community Python is Open Source software; freely distributable Code is owned by Python Software Foundation –501(c)(3) non-profit taking tax-deductible donations –merit-based closed membership (includes sponsors) License is BSD-ish (no "viral" GPL-like clause) Users meet: –on Usenet (comp.lang.python) –on IRC (#python at irc.freenode.net) –at local user groups (e.g. –at conferences (PyCon, EuroPython, OSCON) Website: (downloads, docs, devel)

Sept. 2003© Guido van Rossum 14 Python Development Process Nobody gets paid to work full-time on core Python –Though some folks get paid for some of their time their employers use Python and need enhancements The development team never sleeps –For example, for the most recent release: release manager in Australia key contributors in UK and Germany doc manager and Windows expert in Virginia etc. Key tools: , web, CVS, SourceForge trackers –IRC not so popular, due to the time zone differences

Sept. 2003© Guido van Rossum 15 Python Enhancement Proposals (PEP) RFC-like documents proposing new or changed: –language features –library modules –even development processes Discussion usually starts in python-dev mailing list Wider community discussion on Usenet BDFL approval required to go forward –BDFL = "Benevolent Dictator For Life" (that's me :-) –this is not a democracy; let Python have my quirks –we don't want design by committee or majority rule –the PEP system ensures everybody gets input though

Sept. 2003© Guido van Rossum 16 Python Release Philosophy "Major releases": 2.0 -> 2.1 -> 2.2 -> 2.3 –12-18 month cycle –Focus on new features –Limited backward incompatibilities acceptable usually requires deprecation in previous major release "Minor releases": e.g > > –3-9 month cycle –Focus on stability; zero backward incompatibilities –One previous major release still maintained "Super release": 3.0 (a.k.a. Python 3000 :-) –Fix language design bugs (but nothing like Perl 6.0 :-) –Don't hold your breath (I'll need to take a sabbatical)

Case Study 1: Iterators and Generators "Loops generalized and turned inside out"

Sept. 2003© Guido van Rossum 18 Evolution of the 'For' Loop Pascal:for i := 0 to 9 do... C:for (i = 0; i < 10; i++)... Python:for i in range(10):... General form in Python: for in : Q: What are the possibilities for ?

Sept. 2003© Guido van Rossum 19 Evolution of Python's Sequence Oldest: built-in sequence types: list, tuple, string –indexed with integers 0, 1, 2,... through len(seq)-1 for c in "hello world": print c Soon after: user-defined sequence types –class defining __len__(self) and __getitem__(self, i) Later: lazy sequences: indeterminate length –change to for loop: try 0, 1, 2,... until IndexError Result: pseudo-sequences became popular –these work only in for-loop, not for random access

Sept. 2003© Guido van Rossum 20 Python 1.0 For Loop Semantics for in : Equivalent to: seq = ind = 0 while ind = seq[ind] ind = ind + 1

Sept. 2003© Guido van Rossum 21 Python For Loop Semantics for in : Equivalent to: seq = ind = 0 while True: try: = seq[ind] except IndexError: break ind = ind + 1

Sept. 2003© Guido van Rossum 22 Example Pseudo-Sequence class FileSeq: def __init__(self, filename):# constructor self.fp = open(filename, "r") def __getitem__(self, i):# i is ignored line = self.fp.readline() if line == "": raise IndexError else: return line.rstrip("\n") for line in FileSeq("/etc/passwd"): print line

Sept. 2003© Guido van Rossum 23 Problems With Pseudo-Sequences The __getitem__ method invites to random access –which doesn't work of course –class authors feel guilty about this and attempt to make it work via buffering or raise errors upon out-of-sequence access both of which waste resources The for loop wastes time –passing an argument to __getitem__ that isn't used –producing successive integer objects 0, 1, 2,... (yes, Python's integers are real objects) –(no, encoding small integers as pseudo-pointers isn't faster) »(no, I haven't actually tried this, but it was a nightmare in ABC)

Sept. 2003© Guido van Rossum 24 Solution: The Iterator Protocol (2.2) for in : Equivalent to: it = iter( ) while True: try: = it.next() except StopIteration: break # There's no index to increment!

Sept. 2003© Guido van Rossum 25 Iterator Protocol Design Many alternatives were considered and rejected Can't use sentinel value (list can contain any value) while it.more(): = it.next() –Two calls are twice as expensive as one catching an exception is much cheaper than a call –May require buffering next value in iterator object while True: (more, ) = it.next() if not more: break –Tuple pack+unpack is more expensive than exception

Sept. 2003© Guido van Rossum 26 Iterator FAQ Q: Why isn't next() a method on ? A: So you can nest loops over the same. Q: Is this faster than the old way? A: You bet! Looping over a builtin list is 33% faster. This is because the index is now a C int. Q: Are there incompatibilities? A: No. If doesn't support the iterator protocol natively, a wrapper is created that calls __getitem__ just like before. Q: Are there new possibilities? A: You bet! dict and file iterators, and generators.

Sept. 2003© Guido van Rossum 27 Dictionary Iterators To loop over all keys in a dictionary in Python 2.1: –for key in d.keys(): print key, "->", d[key] The same loop in Python 2.2: –for key in d: print key, "->", d[key] Savings: the 2.1 version copies the keys into a list Downside: can't mutate the dictionary while looping Additional benefit: you can now write "if x in d:" too instead of "if d.has_key(x):" Other dictionary iterators: –d.iterkeys(), d.itervalues(), d.iteritems()

Sept. 2003© Guido van Rossum 28 File Iterators To loop over all lines of a file in Python 2.1: –line = fp.readline() while line: line = fp.readline() And in Python 2.2: –for line in fp: –40% faster than the 'while' loop (which itself is 10% faster compared to Python 2.1) most of the savings due to streamlined buffering using iterators cuts down on overhead and looks better

Sept. 2003© Guido van Rossum 29 Generator Functions Remember coroutines? Or, think of a parser and a tokenizer: –the parser would like to sit in a loop and occasionally ask the tokenizer for the next token... –but the tokenizer would like to sit in a loop and occasionally give the parser the next token How can we make both sides happy? –threads are way too expensive to solve this! Traditionally, one of the loops is coded "inside-out" (turned into a state machine): –code is often hard to understand (feels "inside-out") –saving and restoring state can be expensive

Sept. 2003© Guido van Rossum 30 Two Communicating Loops Generator functions let you write both sides (consumer and producer) as a loop, for example: –def tokenizer(): # producer (a generator) while True:... yield token... –def parser(tokenStream): # consumer while True:... token = tokenStream.next()...

Sept. 2003© Guido van Rossum 31 Joining Consumer and Producer tokenStream = tokenizer(); parser(tokenStream) The presence of yield makes a function a generator The tokenStream object is an iterator The generator's stack frame is prepared, but it is suspended after storing the arguments Each time its next() is called, the generator is resumed and allowed to run until the next yield The caller is suspended (that's what a call does!) The yielded value is returned by next() If the generator returns, next() raises StopIteration "You're not supposed to understand this"

Sept. 2003© Guido van Rossum 32 Back To Planet Earth Generator functions are useful iterator filters Example: double items: A B C D -> A A B B C C D D –def double(it): while True: item = it.next() yield item yield item Example: only even items: A B C D E F -> A C E –def even(it): while True: yield it.next() xx = it.next()# thrown away Termination: StopIteration exception passed thru

Sept. 2003© Guido van Rossum 33 Generators in the Standard Library tokenize module (a tokenizer for Python code) –old API required user to define a callback function to handle each token as it was recognized –new API is a generator that yields each token as it is recognized; much easier to use –program transformation was trivial: replaced each call to "callback(token)" with "yield token" difflib module (a generalized diff library) –uses yield extensively to avoid incarnating long lists os.walk() (directory tree walker) –generates all directories reachable from given root –replaces os.path.walk() which required a callback

Sept. 2003© Guido van Rossum 34 Stop Press! New Feature Spotted! Consider list comprehensions: –[x**2 for x in range(5)] -> [0, 1, 4, 9, 16] Python 2.4 will have generator expressions: –(x**2 for x in range(5)) -> "iter([0, 1, 4, 9, 16])" Why is this cool? –sum(x**2 for x in range(5)) -> 30 computes the sum without creating a list hence faster –can use infinite generators (if accumulator truncates)

Case Study 2: Descriptors "Less dangerous than metaclasses"

Sept. 2003© Guido van Rossum 36 Bound and Unbound Methods As you may know, Python requires 'self' as the first argument to method definitions: –class C:# define a class... def meth(self, arg):#...which defines a method print arg**2 –x = C()# create an instance... –x.meth(5)#...and call its method A lot goes on behind the scenes... NB: classes and methods are runtime objects!

Sept. 2003© Guido van Rossum 37 Method Definition Time A method defined like this: –def meth(self, arg):... is really just a function of two arguments You can play tricks with this: –def f(a, b):# function of two arguments print b –class C:# define an empty class pass –x = C()# create an instance of the class –C.f = f# put the function in the class –x.f(42)# and voila! magic :-)

Sept. 2003© Guido van Rossum 38 Method Call Time The magic happens at method call time Actually, mostly at method lookup time –these are not the same, you can separate them: "xf = x.f; xf(42)" does the same as "x.f(42)" "x.f" is the lookup and "xf(42)" is the call If x is an instance of C, "x.f" is an attribute lookup –this looks in x's instance variable dict (x.__dict__) –then in C's class variable dict (C.__dict__) –then searches C's base classes (if any), etc. Magic happens if: –f is found in a class (not instance) dict, and –what is found is a Python function

Sept. 2003© Guido van Rossum 39 Binding a Function To an Instance Recap: –we're doing a lookup of x.f, where x is a C instance –we've found a function f in C.__dict__ The value of x.f is a bound method object, xf: –xf holds references to instance x and function f –when xf is called with arguments (y, z,...), xf turns around and calls f(x, y, z,...) This object is called a bound method –it can be passed around, renamed, etc. like any object –it can be called as often as you want –yes, this is a currying primitive! xf == "curry(x, f)"

Sept. 2003© Guido van Rossum 40 Magic Is Bad! Why should Python functions be treated special? Why should they always be treated special?

Sept. 2003© Guido van Rossum 41 Magic Revealed: Descriptors In Python 2.2, the class machinery was redesigned to unify (user-defined) classes with (built-in) types –The old machinery is still kept around too (until 3.0) –To define a new-style class, write "class C(object):..." Instead of "if it's a function, do this magic dance", the new machinery asks itself: –if it supports the descriptor protocol, invoke that The descriptor protocol is a method named __get__ __get__ on a function returns a bound method

Sept. 2003© Guido van Rossum 42 Putting Descriptors To Work Static methods (that don't bind to an instance) –a wrapper around a function whose __get__ returns the function unchanged (and hence unbound) Class methods (that bind to the class instead) –returns curry(f, C) instead of curry(f, x) to do this, __get__ takes three arguments: (f, x, C) Properties (computed attributes done right) –__get__ returns f(x) rather than curry(f, x) –__set__ method invoked by attribute assignment –__delete__ method invoked by attribute deletion –(__set__, __delete__ map to different functions)

Sept. 2003© Guido van Rossum 43 Properties in Practice If you take one thing away from this talk, it should be how to create simple properties: –class C(object):# new-style class! __x = 0# private variable def getx(self):# getter function return self.__x def setx(self, newx):# setter function if newx < 0:# guard raise ValueError self.__x = newx x = property(getx, setx)# property definition

Sept. 2003© Guido van Rossum 44 Useful Standard Descriptors Static methods: –class C(object): def foo(a, b):# called without instance... foo = staticmethod(foo) Class methods: –class C(object): def bar(cls, a, b):# called with class... bar = classmethod(bar) See:

Sept. 2003© Guido van Rossum 45 A Schizophrenic Property Challenge: define a descriptor which acts as a class method when called on the class (C.f) and as an instance method when called on an instance (C().f) –class SchizoProp(object): def __init__(self, classmethod, instmethod): self.classmethod = classmethod self.instmethod = instmethod def __get__(self, obj, cls): if obj is None: return curry(self.classmethod, cls) else: return curry(self.instmethod, obj) Do Not Try This At Home! :-)

Question Time "If there's any time left :-)"