Python 3.0 (Py3K) Introduction Authors:timchen119.nospam.at.gmail.com ( 使徒提姆 ) Location: Taiwan COSCUP 2008 Date: 2008/08/24 Blog:

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.
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld.
Bruce Beckles University of Cambridge Computing Service
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
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.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Functions Chapter 4 Python for Informatics: Exploring Information
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
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
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
RUBY by Ryan Chase.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
Python Let’s get started!.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
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.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
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.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Discover Python 3 Slavek Kabrda Presented by
Python 3000.
Development Environment
Python: Experiencing IDLE, writing simple programs
Lecture 2 Python Basics.
Python Let’s get started!.
Introduction to Python
The switch Statement, and Introduction to Looping
7 - Programming 7P, Q, R - Testing.
Intro To Pete Alonzi University of Virginia Library
COMP280:Introduction to Software Development Week 12, Lecture 34
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Refactoring and Code Smells
CHAPTER FOUR Functions.
Writing Functions( ) (Part 5)
Functional.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Introduction to Python
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Functions and Procedures
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Coding Concepts (Basics)
ERRORS AND EXCEPTIONS Errors:
Functional interface.
Testing and Repetition
Python programming exercise
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CSE 231 Lab 4.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Functions Functions being objects means functions can be passed into other functions: sort (list, key=str.upper)
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due today.
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

Python 3.0 (Py3K) Introduction Authors:timchen119.nospam.at.gmail.com ( 使徒提姆 ) Location: Taiwan COSCUP 2008 Date: 2008/08/24 Blog: This document has licensed as CC-SA 2.0

Topic ● Introduction ● New syntax ● New Feature ● Migratation

Introduction ● Python is a dynamic object-oriented programming language that can be used for many kinds of software development. ● Last Stable Release: Python (February 22, 2008) ● Last Beta Release: Python 2.6beta2 and 3.0beta2 (July 17, 2008) ● Python 3000, Python 3.0 and Py3K are all names for the same thing.

Introduction (cont.) ● The actual Python release will be referred as Python 3.0. ● Sep : Python 2.6 and 3.0 final released. (planned) ● Python 2.6: a transitional release to help prepare code for Python 3.0.

Python 3.0 IS... ● backwards-incompatible. ● remove some previous design mistakes. ● reviewing and improving standard library modules. ● django and some python modules may have tools to generate python 2.x/3.0 code from single code base.

What is NOT changed ● Guido said in PEP (PEP: Python Enhancement Proposals) ● Python 3000 will NOT be a rewrite from scratch. ● Python 3000 will NOT be case-insensitive. ● self will NOT become implicit.

What is NOT changed (cont.) ● lambda will NOT be renamed. ● NO braces. (from __future__ import braces) ● The global statement WILL stay.

print() ● from statement to function ● 2.x you can: print("Hello") or print "Hello" ● 3.0 you have to add () ● raw_input(now input) was a function ● print >> open("test.txt","w"), "1","2","3" → (X) ● print("1", "2", "3", file=open("test.txt","w")) → (O)

format() ● in python 2.x:: – t=[1,2,3];"abc%s" % t -> 'abc[1, 2, 3]' – t=(1,2,3);"abc%s" % t -> TypeError ● format() #PEP 3101 ● can define __format__() special method ● "%14.12s" % "hello world! hello!" ==> format("Hello world! Hello World!",">14.12")

Python 3.0 Hello World 101 ● print("hello world!") ● (lambda:print("hello world!"))() ● print("hello {what}".format(what='world!')) ● print("{0} {1}".format('hello','world!')) ● print("hello {0.name}".format(open('world!', 'w'))) ● print("hello {0[what]}".format(dict(what='world!'))) ● print("hello {0[what]}".format({'what':'world!'}))

unicode ● unicode(charset) is not utf-8 (bytestring). ● unicode == str now. (no unicode type in 3.0) ● add a bytes type, bytes('test','utf-8') ● ' 開源 '.encode('utf-8')==> b'\xe9\x96\x8b\xe6\xba\x90' ● ' 開源 '.encode('cp950')==> b'\xb6}\xb7\xbd' ● Unicode(2.x) -> str(3.x) ● str(2.x) -> bytes(3.x)

abc,collections ● PEP 3119 ● Abstract Base Class ● module collections ● OOP term 'Invocation' : object.method() ==> implies polymorphis ● polymorphism: 'type-of object'.method() : may run different code when types different. ● duck typing polymorphism: assume object is a duck if it can quacks. -- inspection style, check from external. (did not use object's method to check)

a situation without using abc ● Situation: you want to accept iterables from one of following datatypes: dict or tuples. ● you use hasattr(my_iterables,'items') ● may lead to unexpected behavior. ● does it support dict-like objects? isn't that main advantages to use duck-typing ? ● what is a dict?

abc,collections (cont.) ● there maybe a dict without __contains__ and have has_key() function. ● here comes abc ==> Abstract base classes ● what to inspect? there are too many stuffs in a python object to check. ● abc and collection modules intends to answer these questions: – what is a 'MutableMapping' ? – what is a 'Iterable' ? – and what is a 'MyAbstractType' ?

abc,collections (cont.) ● isinstance([],

abc example ● ● Python2 try: iter(obj) except TypeError: do_something_with_not_iterable_object(obj) else: do_something_with_iterable_object(obj)

abc example (cont.) ● Python3 from collections import Iterable if isinstance(obj, Iterable): the_object_is_iterable() else: the_object_is_not_iterable()

abc (cont.) ● your custom class doesn't need to inherit from the base class. ● how to do it? ● collections.Iterable.register(myclass) ● how to register your module to abc: from _yourlinkedlist import YourLinkedList from collections import Sequence Sequence.register(YourLinkedList)

abc (cont.) ● your C extension module works great. ● io.IOBase, io.BufferedIOBase ● StringIO,cStringIO --> io ● abc --> the pythonic way to do both traditional OOP and duck-typing polymorph in the same time. ● "There should be one and preferably only one obvious way to do it".

nonlocal ● PEP 3104 ● python 2.1+ name resolution: LEGB (local => enclosed => global => builtins) ● nested scoping was added when python 2.1 introduced. ● local was always first checked, so if you assigned a local varible in a scope it assume that 'name' is a local, it's too bad for a closure. ● there was some working arounds, however python 3.0 add a keyword: nonlocal.

closure (it's read-only) ● python 2.x def get_counter_py2(): x = 0 def f(): x += 1 return x return f add1 = get_counter_py2() print add1();print add1();print add1()

closure (it's read-only) python 2.x error:: Traceback (most recent call last): File "counter.py", line 10, in print next() File "counter.py", line 4, in f x += 1 UnboundLocalError: local variable 'x' referenced before assignment

closure(cont.) closure working around in 2.x (1):: def get_counter_traditional(): x = [0] def f(): x[0] += 1 return x[0] return f

closure(cont.) closure working around in 2.x (2):: def get_counter_neat(): def f(): f.x += 1 return f.x f.x = 0 return f ● "To describe something as clever is NOT considered a compliment in the Python culture." -- Alex Martelli (Python Cookbook 2nd.)

closure with nonlocal(cont.) py3k used nonlocal:: def get_counter_py3k(): x=0 def f(): nonlocal x x += 1 return x return f add1 = get_counter_py3k() print(add1());print(add1());print(add1())

closure with nonlocal(cont.) py3k used nonlocal:: def get_counter_py3k(x): def f(): nonlocal x x += 1 return x return f next = get_counter_py3k(1) print(add1());print(add1());print(add1()) * "There should be one and preferably only one obvious way to do it".

function annotations ● PEP completely optional. ● def foo(a: 'x', b: 5 + 6, c: list = [5,2,3]) -> max(2, 9): pass ● foo.__annotations__ -> {'a': 'x', 'c':, 'b': 11, 'return': 9} ● you could think as a compile time comment for function. ● intend to used by third-party libraries. ie: typecheck ● "There should be one and preferably only one obvious way to do it".

Misc. __package__:: PEP Main module explicit relative imports Extended unpacking:: x,*y = range(6) a,b,*c = {1:2,3:4,5:6}

Misc. (cont.) map() filter() reduce() and list compresion:: map() and filter() becomes iterator and still in builtins reduce() placed in functools module range():: next(range(10)) : TypeError: range object is not an iterator

migrate py3k use 2to3 * /usr/bin/2to3 (/usr/local/bin/2to3) 2to3 -w 2.py:: py (original) py (refactored) -1,1 +1,1 -print 'hi' +print('hi') RefactoringTool: Files that were modified: RefactoringTool: 2.py

Conclude ● a lot of stuff changed. (there're still lots of stuff we don't mention changed!) ● you could still use python 2.x and wait python 3.x matures. ● 2.6+ will recieve backports from python 3.0 ● from __future__ import py3k