What's New in Python 2.2 LinuxWorld - New York City - January 2002 Guido van Rossum Director of PythonLabs at Zope Corporation

Slides:



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

® ® Why Design Another Language? Python UK & ACCU Spring Conference Oxford - April 2, 2003 Guido van Rossum Director of PythonLabs at Zope Corporation.
Reston/San Diego, July 25, O'Reilly Open Source Convention 2001 San Diego, CA Python Track Keynote Guido van Rossum Zope Corporation
"The State of the Python Union" Python10 - Alexandria, VA - February 7, 2002 Guido van Rossum Director, PythonLabs at Zope Corporation
State of the Python Union OSCON, July 24, 2002 Guido van Rossum Director of PythonLabs at Zope Corporation
Python 3000 (PyCon, 24-Feb-02007) Guido van Rossum
Python 3000 and You Guido van Rossum PyCon March 14, 2008.
director of PythonLabs at Zope Corporation
Python Regrets OSCON, July 25, 2002
EuroPython Keynote June 26, 2002 Guido van Rossum Director of PythonLabs at Zope Corporation
Introduction to Python LinuxWorld - New York City - January 2002 Guido van Rossum Director of PythonLabs at Zope Corporation
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Bruce Beckles University of Cambridge Computing Service
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Introduction to Python Guido van Rossum Director of PythonLabs at Zope Corporation
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
The Python Programming Language Jeff Myers Programming Language Concepts, 01/14/2002
CS Final Project 2010 Rohan Paramesh. The Programs  Implementing 3 programs each in C# and Python  Student Scheduler  Assigns courses and students,
By the end of this session you should be able to...
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
Python Overview  Last week Python 3000 was released  Python 3000 == Python 3.0 == Py3k  Designed to break backwards compatibility with the 2.x.
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.
By: Ben Hester. What is Python Powerful Dynamic Programming Language Uses Whitespace Everything Is A Object Very Portable.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
 A readable, dynamic, pleasant,  flexible, fast and powerful language Introduction to Python.
Python Documentation Fran Fitzpatrick. Overview  Comments  Documentation Strings  Pydoc  Comments  Documentation Strings  Pydoc.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
Return to Home! Go To Next Slide! Return to Home! Go To Next Slide!
Python Programming Language by Vasu Chetty. Origins of Python Created by: Guido van Rossum, a Dutch Programmer Created during: Christmas Break, 1989 Created.
Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2 # The following python statement has python variables which are intended to represent the fact.
Whatcha doin'? Aims: To start using Python. To understand loops.
Computer Programming Fundamentals
ECS10 10/10
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Introduction to Python
Count Controlled Loops (Nested)
Passing Parameters by value
Practice with loops! What is the output of each function below?
Lesson 06: Functions Class Chat: Attendance: Participation
Lecture 7: Python’s Built-in Types and Basic Statements
Functions That Do Not Return a Value
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Programming II Vocabulary Review Loops & functions
Presentation transcript:

What's New in Python 2.2 LinuxWorld - New York City - January 2002 Guido van Rossum Director of PythonLabs at Zope Corporation

Slide 2©2001, 2002 Guido van Rossum Overview Appetizers –Nested Scopes –Int/Long Unification –Integer Division First Course –Iterators –Generators Second Course –Type/Class Unification Dessert –Metaclass Programming

Slide 3©2001, 2002 Guido van Rossum Nested Scopes Motivation: –functions inside functions need to access outer locals Optional in 2.1 –from __future__ import nested_scopes Standard in 2.2 –Can't turn it off (__future__ statement still allowed) Only issue to watch out for: –def f(str): def g(x): print str(x)... –In 2.1, str argument independent from str() function –In 2.2, str inside g() references outer str

Slide 4©2001, 2002 Guido van Rossum Int/Long Unification 2**1000 no longer raises OverflowError –but returns the appropriate long (no 'L' needed!) Ditto for: – * –sys.maxint + 1 – But: –1<<100 is still zero –repr(2**1000) still ends in 'L' –hex(), oct(), %x, %o, %u differ for int vs. long –These will change in 2.3 or 3.0

Slide 5©2001, 2002 Guido van Rossum Integer Division Motivation: –x/y computes different thing for ints than for floats This is unique among operators, and confusing –def velocity(dist, time): return dist/time Lack of type declarations makes this hard to debug Solution: –x//y new, "floor division": always truncates (to -Inf) –x/y unchanged, "classic division" –from __future__ import division x/y returns "true division": always a float value In Python 3.0, x/y will be true division –Use Tools/scripts/fixdiv.py tool for conversion help

Slide 6©2001, 2002 Guido van Rossum Iterators Motivation: –Generalized for loop innards, doing away with index Iterator abstracts state of for loop –Sequence may be a "real" sequence (e.g. a list) –Or a "virtual" sequence (e.g. the nodes of a tree) Most iterator calls are implied by for loops –For loop gets items one at a time from next() –Exception StopIteration signals end of sequence You can also call next() explicitly

Slide 7©2001, 2002 Guido van Rossum Iterator API t = iter(obj) returns a (new) iterator for obj –Calls t = obj.__iter__(); obj must provide __iter__ t.next() returns the next value –Raises StopIteration when there is no next value Alternatives rejected (t.end(), if t, t.next() == None) The iterator should be a separate object –Allow for multiple iterations over the same object e.g. nested loops, multiple threads, nested calls But when t is an iterator: –iter(t) is t –This is handy with "for x over :..."

Slide 8©2001, 2002 Guido van Rossum The 'for' Loop for x in obj: f(x) is now translated (roughly) as follows: t = iter(obj) while 1: try: x = t.next() except StopIteration: break f(x)

Slide 9©2001, 2002 Guido van Rossum Built-in Iterators for item in sequence:... # nothing new :-) for line in file:... # most efficient way! for key in dict:... # must not modify dict! –for key, value in dict.iteritems():... –(unrelated but also new: if key in dict:...) for val in iter(callable, endval):... –while 1: val = callable() if val == endval: break... –Example: iter(file.readline, "")

Slide 10©2001, 2002 Guido van Rossum Generators Motivation: –Function to produce a sequence, one item at a time State represented by local variables and/or PC Using an object is overkill or inconvenient Example: –from __future__ import generators –def fibonacci(a=1, b=1): while 1: yield a a, b = b, a+b –t = fibonacci() # t is an iterator! –for i in range(10): print t.next()

Slide 11©2001, 2002 Guido van Rossum How Does It Work? Stack frame is created in suspended state –Arguments in place, but no byte code executed yet t is a wrapper pointing to the suspended frame –t supports the iterator interface Calling t.next() resumes the stack frame –Execution continues where it left off previously A yield statement suspends the stack frame –Yield "argument" is the returned from t.next() An exception terminates the stack frame –Propagates out normally –Return (or falling through) raises StopIteration

Slide 12©2001, 2002 Guido van Rossum Examples: "Iterator Algebra" def alternating(a): ta = iter(a) while 1: ta.next(); yield ta.next() def zip(a, b): ta = iter(a); tb = iter(b) while 1: yield (ta.next(), tb.next()) for x, y in zip("ABC", "XYZ"): print x+y For a real-life example, see tokenize.py

Slide 13©2001, 2002 Guido van Rossum Type/Class Unification Subclassing built-in types like dict or list "Cast" functions are now types, acting as factories Built-in objects have __class__, types have __dict__ Overriding __getattr__() Descriptors and the __get__() operation property() classmethod(), staticmethod() super() and the new method resolution order (MRO) Subclassing immutable types: __new__() Performance hacks: __slots__ Metaclasses

Slide 14©2001, 2002 Guido van Rossum Subclassing Built-ins class mydict(dict): def keys(self): K = dict.keys(self) K.sort() return K class mylist(list): def __sub__(self, other): L = self[:] for x in other: if x in L: L.remove(x) return L These are "new-style" because of their base classes Note: self is an instance of the base class; the subclass is not a wrapper like UserDict or UserList

Slide 15©2001, 2002 Guido van Rossum "Cast" Functions These built-ins are now types instead of factory functions (with the same signature): –int, long, float, complex –str, unicode –tuple, list –open (now an alias for file) –type These are new as built-in names: –dict, file –object: the universal base class (new-style) Useful new idiom: if isinstance(x, file) etc.

Slide 16©2001, 2002 Guido van Rossum Unified Introspection obj.__class__ == type(obj) –Exception for unconverted 3rd party extension types Types have __dict__, __bases__ All methods and operators shown in __dict__ –E.g. list.__dict__ contains 'append', '__add__' etc. –list.append is an "unbound method": list.append(a, 12) same as a.append(12) list.append.__doc__ yields the doc string list.__bases__ == (object,) list.__class__ is type

Slide 17©2001, 2002 Guido van Rossum Overriding __getattribute__ class mylist(list): def __getattribute__(self, name): try: return list.__getattribute__(self, name) except AttributeError: return "Hello World" The __getattribute__ method is always called –Not just when the attribute isn't found –Classic __getattr__ also available on new-style classes Do not use self.__dict__[name] –Call the base class __getattribute__ or __setattr__

Slide 18©2001, 2002 Guido van Rossum Descriptors Generalization of unbound methods –Used by new-style object getattr, setattr –Also by classic instance getattr Descriptor protocol: __get__(), __set__(), __delete() descr.__get__(object) is binding operation –invoked by getattr when descriptor found in class –e.g. function or unbound method -> bound method descr.__get__(None, class): unbound method descr.__set__(object, value) –invoked by setattr when descriptor found in class __get__() is also used by classic classes!

Slide 19©2001, 2002 Guido van Rossum Properties class C(object): def get_x(self): return self.__x def set_x(self, value): self.__x = value x = property(get_x, set_x, doc="...") a = C() a.x # invokes C.get_x(a) a.x = 1 # invokes C.set_x(a, 1) –Descriptor overrides attribute assignment C.x.__doc__ == "..." You can leave out set_x, or add del_x

Slide 20©2001, 2002 Guido van Rossum Static Methods class C: def spawn(): return C() spawn = staticmethod(spawn) c1 = C. spawn() c2 = c1.spawn() Use is just like in Java Syntax is ugly, provisional –Python 2.3 may bring new syntax

Slide 21©2001, 2002 Guido van Rossum Class Methods [Skip if running out of time] Similar to static methods, but get class arg: class C: def spawn(cls): return cls() spawn = classmethod(spawn) class D(C): pass c1 = C.spawn(); c2 = c1.spawn() d1 = D.spawn()

Slide 22©2001, 2002 Guido van Rossum Superclass Method Calls class A(object): def save(self, f): "save state to file f"... class B(A): def save(self, f): super(B, self).save(f) # instead of A.save(self, f)... Motivation: see following slides Verbose syntax: may be fixed later

Slide 23©2001, 2002 Guido van Rossum Cooperative Methods Now it gets interesting: class C(A): def save(self, f): super(C, self).save(f)... class D(B, C): def save(self, f): super(D, self).save(f) D().save(f) –D.save() -> B.save() -> C.save() -> A.save() !!! –This will be explained shortly :-)

Slide 24©2001, 2002 Guido van Rossum CB D A Diamond Diagram

Slide 25©2001, 2002 Guido van Rossum...But How...?!?! How does super(B, self).save(f) know to call C.save(f) when self is a D instance?!?! Answer: linearized MRO stored as D.__mro__ –MRO = Method Resolution Order (see next slide) D.__mro__ == (D, B, C, A) super(B, self).save looks for B in self.__mro__ and looks for save in classes following it –It searches (C, A) for save Specifically it looks for C.save and A.save in that order

Slide 26©2001, 2002 Guido van Rossum Method Resolution Order Used for method lookup in new-style classes Compare to classic MRO: –classic MRO: left-to-right, depth-first (D, B, A, C, A) –new MRO removes duplicates from the left Motivation: –In diamond diagram, C.save should override A.save If B and D don't define save, D.save should find C.save –This is more important because of 'object' The universal base class for all new-style classes

Slide 27©2001, 2002 Guido van Rossum Immutable Types Override __new__ instead of __init__ __new__ is a static method with a class arg! tuple(arg) calls tuple.__new__(tuple, arg) class mytuple(tuple): def __new__(cls, *args): return tuple.__new__(cls, args) t = mytuple(1, 2, 3)

Slide 28©2001, 2002 Guido van Rossum __slots__ Allocates instance variables in the object structure instead of using a pointer to a __dict__; saves a lot of space class C(object): __slots__ = ['foo', 'bar'] def __init__(self, x, y): self.foo = x; self.bar = y c1 = C(1, 2) c1.spam = 12 # error

Slide 29©2001, 2002 Guido van Rossum Incompatibilities dir() behaves differently –shows instance variables and methods –shows methods from base classes as well –exceptions: dir(module) returns only __dict__ contents dir(class_or_type) doesn't look in the metaclass type("").__name__ == "str" # was "string" type(1L).__name__ == "long" # "long int"

Slide 30©2001, 2002 Guido van Rossum Metaclass Programming class autosuper(type): def __init__(cls, name, bases, dict): attrname = '_%s__super' % name setattr(cls, attrname, super(cls)) class myclass(object): __metaclass__ = autosuper def foo(self): self.__super.foo()...etc... myclass.__class__ is autosuper

Slide 31©2001, 2002 Guido van Rossum Questions Now or never :-)