Python 3000 (PyCon, 24-Feb-02007) Guido van Rossum

Slides:



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

Python 3000 and You Guido van Rossum PyCon March 14, 2008.
Python 3000 (ACCU Conference / Oxford, UK / April 19, 2006) Guido van Rossum
Python Regrets OSCON, July 25, 2002
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Container Types in Python
Bruce Beckles University of Cambridge Computing Service
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Data Representation Kieran Mathieson. Outline Digital constraints Data types Integer Real Character Boolean Memory address.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CIS Computer Programming Logic
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Input, Output, and Processing
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
1 C++ Syntax and Semantics, and the Program Development Process.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Lecture 4 Python Basics Part 3.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python C API overview References:
Python 3000.
Topics Introduction to File Input and Output
More About Data Types & Functions
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Object Oriented Programming in java
CSC1018F: Intermediate Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Lists Part 1 Taken from notes by Dr. Neil Moore
Python Primer 1: Types and Operators
C Programming Getting started Variables Basic C operators Conditionals
functions: argument, return value
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
COMPUTER PROGRAMMING SKILLS
Chap 2. Identifiers, Keywords, and Types
Topics Introduction to File Input and Output
Presentation transcript:

Python 3000 (PyCon, 24-Feb-02007) Guido van Rossum

24 Feb 02007(c) 2007 Python Software Foundation2 What Is Python 3000? The next major Python release –To be released as Python 3.0 The first one in a long time to be incompatible –But not completely different or unusual Concept first formed around 2000 –Py3k nickname was a play on Windows 2000 Goal: to correct my early design mistakes –Those that would require incompatibility to fix –Reduce cognitive load for first-time learners Work and thinking started for real last year

24 Feb 02007(c) 2007 Python Software Foundation3 Activity Since Last Year Lots of design discussions –(too many, if you ask me :-) Some PEPs were written –(but not enough…) Lots of code was written –(just the right amount!) –(but we're not done yet!!)

24 Feb 02007(c) 2007 Python Software Foundation4 Python 3.0 Timeline PEPs to be completed: April a1: June final: June 2008 For comparison, the 2.6 timeline: 2.6a1: December final: April 2008 There will also be a 2.7 timeline

24 Feb 02007(c) 2007 Python Software Foundation5 Rest of the Talk Highlight some of the most visible changes –print function, dict views, comparisons, unicode, … How to convert 2.x to 3.0 code Notational convention: *= incompletely implemented **= not yet implemented

24 Feb 02007(c) 2007 Python Software Foundation6 No More Classic Classes In 2.2 … 2.9: –class C:# classic class (0.1 … 2.1) –class C(object):# new-style class (old now :-) In 3.0: –both are new-style classes (just say "classes") Differences are subtle, few of you will notice

24 Feb 02007(c) 2007 Python Software Foundation7 Print is a Function print x, y-> print(x, y) print x,-> print(x, end=" ") print >>f, x-> print(x, file=f) Automatic translation is 98% correct Fails for cases involving softspace cleverness: –print "x\n", "y" doesn 't insert a space before y –print("x\n", "y") does –ditto for print "x\t", "y"

24 Feb 02007(c) 2007 Python Software Foundation8 Dictionary Views Inspired by Java Collections Framework Remove.iterkeys(),.iteritems(),.itervalues() Change.keys(),.items(),.values() These return a dict view –Not an iterator –A lightweight object that can be iterated repeatedly –.keys(),.items() have set semantics –.values() has "collection" semantics supports iteration and not much else

24 Feb 02007(c) 2007 Python Software Foundation9 Default Comparison Changed Default ==, != compare object identity –(this is unchanged) Default, >= raise TypeError Example: [1, 2, ""].sort() raises TypeError Rationale: 2.x default ordering is bogus –depends on type names –depends on addresses

24 Feb 02007(c) 2007 Python Software Foundation10 **Unicode Strings Java-like model: –strings (the str type) are always Unicode –separate bytes type –must explicitly specify encoding to go between these Open issues: –implementation fixed-width characters for O(1) indexing maybe 3 internal widths: 1, 2, 4 byte characters C API issues (many C APIs use C char* pointers) –optimize slicing and concatenation??? lots of issues, supporters, detractors

24 Feb 02007(c) 2007 Python Software Foundation11 The Bytes Type A mutable sequence of small ints (0…255) –b[0] is an int; b[:1] is a new bytes object Implemented efficiently as unsigned char[] Has some list-like methods, e.g..extend() Has some string-like methods, e.g..find() –But none that depend on locale bytes literals: b"ascii or \xDD or \012" bytes has.decode() method returning a string str has a.encode() method returning bytes

24 Feb 02007(c) 2007 Python Software Foundation12 **New I/O Library Stackable components (inspired by Java, Perl) –Lowest level: unbuffered byte I/O platform-specific; don't use C stdio –Add buffering –Add unicode encoding/decoding encoding explicitly specified or somehow guessed –Add CRLF/LF mapping Compatible API –open(filename) returns a buffered text file read() and readline() return strings –open(filename, "b") returns a buffered binary file read() returns bytes; can't use readline()

24 Feb 02007(c) 2007 Python Software Foundation13 Int/Long Unification There is only one built-in integer type Its name is int Its implementation is like long in Python 2.x C API is a bit murky Performance could use a boost

24 Feb 02007(c) 2007 Python Software Foundation14 Int Division Returns a Float Always! Same effect in 2.x with –from __future__ import division Use // for int division Use -Q option to Python 2.x to find old usage

24 Feb 02007(c) 2007 Python Software Foundation15 **Raise and Except Changes All exceptions must derive from BaseException Exceptions have __traceback__ attribute Must use raise E(arg) instead of raise E, arg Can still use raise E and raise without args Use raise E(arg).with_traceback(tb) –instead of raise E, arg, tb Use "except E as v:" instead of "except E, v:" Variable v is deleted at end of except block!!!

24 Feb 02007(c) 2007 Python Software Foundation16 Signature Annotations NOT type declarations! Example: –def foo(x: "whatever", y: list(range(3))) -> 42*2: … Argument syntax is (roughly): –NAME [':' expr] ['=' expr] Both expressions are evaluated at 'def' time –foo.func_annotations is: {'a': "whatever", 'b': [0, 1, 2], "return": 84} –NO other use is made of these annotations

24 Feb 02007(c) 2007 Python Software Foundation17 Keyword-Only Parameters Example def: –def foo(a, b=1, *, c=42, d): … Example call: –foo(1, 2, d=3) Cannot use: –foo(1, 2, 3) # raises TypeError

24 Feb 02007(c) 2007 Python Software Foundation18 Set Literals {1, 2, 3} is the same as set([1, 2, 3]) No empty set literal; use set() No frozenset literal; use frozenset({…}) **Set comprehensions: –{f(x) for x in S if P(x)} same as set(f(x) for x in S if P(x))

24 Feb 02007(c) 2007 Python Software Foundation19 Absolute Import Same effect in 2.5 with –from __future__ import absolute_import Within a package "import foo" does NOT search the package path, only sys.path Use "from. import foo" for relative import Or use from import foo

24 Feb 02007(c) 2007 Python Software Foundation20 **String Formatting Examples (see PEP 3101 for more): –"See {0}, {1} and {foo}".format("A", "B", foo="C") "See A, B and C" –"my name is {0} :-{{}}".format("Fred") "my name is Fred :-{}" –"File name {0.foo}".format(open("foo.txt")) File name foo.txt –"Name is {0[name]}".format({"name": "Fred"}) "Name is Fred" –Shoe size {0:8}".format(42) "Shoe size 42"

24 Feb 02007(c) 2007 Python Software Foundation21 **Nonlocal Statement def outer(): x = 42 def inner(): nonlocal x# <---- new print(x) x += 1 return inner Doesn't work today; x becomes a local in inner Different keywords proposed: –nonlocal, global, outer, … (see PEP 3104)

24 Feb 02007(c) 2007 Python Software Foundation22 **Abstract Base Classes? Still highly speculative (no PEP yet) –wiki.python.org/moin/AbstractBaseClasses Introduce a standard abstract class hierarchy for type categories like file, container, sequence, iterable etc. Standard types to use these as base classes User-defined types may use these When used, can help distinguishing e.g. sequence from mapping, or file-like behavior, or "stringiness", or "numericity", etc.

24 Feb 02007(c) 2007 Python Software Foundation23 **Switch/Case Statement??? Highly speculative; see PEP 3103 –switch EXPR: case EXPR: SUITE case EXPR:# or case in EXPRLIST: SUITE … [else: SUITE] Problem: when to compile EXPR? –Would prefer precompilation for faster execution –But this would introduce unusual semantics

24 Feb 02007(c) 2007 Python Software Foundation24 Miscellaneous Changes exec becomes a function again range() becomes xrange() input() becomes raw_input() zip() returns an iterator Moved intern() into sys module Renamed __nonzero__ to __bool__ 'as' and 'with' are keywords And more, planned and implemented

24 Feb 02007(c) 2007 Python Software Foundation25 Miscellaneous Removals classic classes: new-style classes default backticks: use repr() Removed <>: use != apply(): use func(*args) coerce(), __coerce__: not needed dict.has_key(): use key in dict 'softspace' attribute on file objects

24 Feb 02007(c) 2007 Python Software Foundation26 **Library Reform Not my priority Others are interested, but effort seems stalled Need help! May happen after 3.0a1 is released

24 Feb 02007(c) 2007 Python Software Foundation27 *C API Changes Too early to tell what will happen 3rd party extension authors want to know For now, these simple rules: –Adding APIs is okay (of course) –Deleting APIs is okay –Changing APIs incompatibly is NOT OKAY

24 Feb 02007(c) 2007 Python Software Foundation28 Converting 2.x Code to 3.0 Generic conversion tool exists –sandbox/2to3 –accurate source-to-source transformation –parse tree decorated with whitespace & comments New conversions are easily added –create a class from boilerplate –add a class variable PATTERN to match nodes –add a method transform() to transform one node Separately, Python 2.6 will help –can warn about out-of-date usages –can provide forward-compatible alternatives

24 Feb 02007(c) 2007 Python Software Foundation29 Examples of What It Can Do apply(fun, args, kwds) -> fun(*args, **kwds) d.iterkeys() -> d.keys() exec a in b, c -> exec(a, b, c) print >>sys.stderr, x, -> print(x, end=" ", file=sys.stderr) except E, v: -> except E as v: d.has_key(k) -> k in d intern(s) -> sys.intern(s) a <> b -> a != b; `x` -> repr(x); int -> long automatically adds parentheses where needed

24 Feb 02007(c) 2007 Python Software Foundation30 Examples of What It Can't Do detect whether d is a dict (in d.iterkeys()) detect whether you use d.keys() as a list later turn int()/int() into int()//int() fix code that depends on int() < str() remove redundant code fix custom classes emulating dictionaries fix string exceptions, non-Exception exceptions in general: limited to syntactic conversions –can't follow control flow, doesn't do type inference

24 Feb 02007(c) 2007 Python Software Foundation31 What You Can Do Today Don't worry about stuff that can be automated Don't try to write source-level compatible code –Use Python 2.6 when it comes out –Write unit tests with maximal coverage –Use keys = sorted(d.iterkeys()) –Use list(d.iterkeys()) when you really need a list –Derive all exceptions from Exception –Derive all classes from object –Don't rely on subtle print/softspace semantics use print line.rstrip("\n") instead of print line, –Use // for int division

Questions