What's New in Python

Download Report

Transcript What's New in Python

State of the
Python Union
OSCON
Portland, Oregon
July 29, 2004
Guido van Rossum
Elemental Security, Inc.
[email protected]
[email protected]
July 29, 2004
© 2004 Guido van Rossum
2
Keynote Overview
• PSF Grants
• Release status
• Python 2.4:
– Generator expressions
– Decorators
– Other news
• Beyond Python 2.4
• Miscellaneous remarks
• Cute demos
• Question time
July 29, 2004
© 2004 Guido van Rossum
3
July 29, 2004
© 2004 Guido van Rossum
4
PSF Grants
• Grants for projects related to:
– the further development of Python
– Python-related technology
– educational resources
• Proposals to be submitted by October 1, 2004
– proposals granted by November 1, 2004
– work to be completed by October 30, 2005
• Up to $40,000 available in total
• See python.org for details and how to submit
July 29, 2004
© 2004 Guido van Rossum
5
July 29, 2004
© 2004 Guido van Rossum
6
Release Status
• Python 2.2 is resting ("pining for the fjords")
• Python 2.3 is actively maintained
– 2.3.4 came out in May
– 2.3.5 planned later this year
– Anthony Baxter is release manager
• I am out of the loop!
• Python 2.4 alpha 2 to be released next week
– 2.4 final release expected Q4 2004
– Anthony Baxter is release manager
• I am mostly out of the loop!
July 29, 2004
© 2004 Guido van Rossum
7
July 29, 2004
© 2004 Guido van Rossum
8
Generator Expressions
• Consider: print sum(x**2 for x in range(10))
• Compare:
– total = 0
for x in range(10):
total += x**2
print total
• Factor out summing algorithm for reuse
• Concentrate on providing input without distractions
• Easily switch between different data processors
– "accumulator functions"
– "iterator algebra" a.k.a. pipelining
July 29, 2004
© 2004 Guido van Rossum
9
Why Generator Expressions?
• Notation is immediately understandable
• Avoids building up a list of intermediate results
– This is especially important if:
• intermediate results are large; or
• there are many intermediate results; or
• the source is an infinite sequence
– e.g. from itertools: count(), cycle(), repeat()
• the consumer doesn't consume all results
• an interactive user is waiting for the initial results
• List comprehensions are a special case:
– [f(x) for x ...] is syntactic sugar for list(f(x) for x ...)
• (in Python 2.4 there are some corner cases with different
semantics; these will be fixed in 3.0)
• won't go away
July 29, 2004
© 2004 Guido van Rossum
10
The Generator Expression Debate
• Someone noticed an "implementation flaw":
– a = []
for f in [math.sin, math.cos, math.tan]:
a.append(f(x) for x in range(10))
– for iterator in a:
for x in iterator:
print x,
print
– Prints math.tan(x) series three times!
(f is bound late)
• Should we try to fix this? (If so, how?)
• NO! Genexps are for immediate consumption
– fix the docs / tutorials / examples
– delayed use would be expert/advanced use anyway
• working solutions are quite bearable
July 29, 2004
© 2004 Guido van Rossum
11
July 29, 2004
© 2004 Guido van Rossum
12
Decorators
• Consider (introduced in python 2.2):
– class C:
def func(args):
...100 lines of body text...
func = staticmethod(func)
• Problem with this syntax:
– programmer may forget to add the staticmethod() call
– reader may miss the staticmethod() call
• Proposed solution:
– class C:
***DECORATOR SYNTAX*** func(args): body
July 29, 2004
© 2004 Guido van Rossum
13
What Is a Decorator?
• A decorator is a meta-function:
– input is a callable or descriptor (implements __get__)
– output is a callable or descriptor
– examples: classmethod, staticmethod, (property)
• Other use cases:
– metadata (e.g. author, version, deprecation)
– processing rules (e.g. SPARK's grammar rules)
– support for external language bindings (ObjC, C#)
– framework annotations (e.g. PIKE)
• Ideally, decorators should be chainable
July 29, 2004
© 2004 Guido van Rossum
14
Decorator Syntax Candidates
• Java 1.5:
– @decorator and/or @decorator(key=value, ...)
• C#:
– [decorator, decorator, ...]
• Other proposals from Python developers:
– [decorator, decorator, ...] in various other positions
– *[decorators]*
– <decorators>
– [as decorators]
– other keywords or symbols
July 29, 2004
© 2004 Guido van Rossum
15
Decorator (Ab)uses
• def funcattrs(**kwds):
def helper(func):
func.__dict__.update(kwds)
return func
return helper
• Example with Java 1.5-derived syntax:
– @funcattrs(grammar="blah", author="GvR")
def blah(args):
body
• Example with C#-derived syntax:
– [funcattrs(grammar="blah", author="GvR")]
def blah(args):
body
July 29, 2004
© 2004 Guido van Rossum
16
Which Decorator Syntax?
• All styles have their disadvantages:
– @decorator
• looks unpythonic (?)
– [deco, deco, ...] prefix
• ambiguous syntax
• and doesn't work in interactive interpreter
– [deco, deco, ...] after arguments
• hides the decorators too much
• is awkward for long decorators
– Other proposals look arbitrary (even 'as')
• In 2.4a2, we'll implement @decorator
– if everybody hates it, we'll revisit in a3 or b1
July 29, 2004
© 2004 Guido van Rossum
17
July 29, 2004
© 2004 Guido van Rossum
18
What Else is New in 2.4?
• Faster (e.g. tklife.py gains ~20% speed-up)
• New built-in set types: set and frozenset
• Unifying int and long: now always the same results
– except repr() still returns an 'L' suffix for longs
• New builtins: sorted(), reversed()
• Keyword arguments to list.sort(): cmp, key, reverse
• eval() takes arbitrary mapping for locals (only)
• None is a constant (assigning to it is a syntax error)
• Decimal floating point data type (module decimal)
• Email parser rewritten from scratch
• CJKCodecs integrated (East-Asian codecs)
• New module cookielib (client-side cookie handling)
• Windows distribution now built using MSVC++ 7.1
July 29, 2004
© 2004 Guido van Rossum
19
July 29, 2004
© 2004 Guido van Rossum
20
Beyond Python 2.4
• Python 2.5.........2.9:
– gradual improvements
– new library modules/packages
– backwards-compatible changes
– implement more existing PEPs
– performance work
– implementation internals work (e.g. AST branch)
– experiment with features proposed for Python 3.0
• There will not be a Python 2.10
– but 2.5 ... 2.9 may be released in parallel with 3.0
July 29, 2004
© 2004 Guido van Rossum
21
July 29, 2004
© 2004 Guido van Rossum
22
Python 3.0 (a.k.a. Python 3000)
• I had a Python 3.0 slide, but it's all day-dreaming
• 3.0 is my excuse for putting off thorny issues
• Has been "about 3 years away" since 2000 :-)
• I need to retire to be able to work on this
• Python 3.0 : 2.x isn't anything like Perl 6.0 : 5.x
• But it will be incompatible
July 29, 2004
© 2004 Guido van Rossum
23
July 29, 2004
© 2004 Guido van Rossum
24
Miscellaneous Remarks
• The sincerest form of flattery is imitation
– Python borrows from many other languages
– Now Ruby, Groovy, Prothon borrow from Python
• Python runs on Nokia Series 60 phones
– Public release not yet certain
– Would open up Nokia platform to more developers
• Write your secure programs in Python
– 1 security defect per 1000 lines across languages
– But in Python it takes fewer lines :-)
• Community issues
– Why is there still no Python equivalent of CPAN?
– There are too many projects doing X; how to choose?
July 29, 2004
© 2004 Guido van Rossum
25
July 29, 2004
© 2004 Guido van Rossum
26
Cute Demos
• ElementClass
– Simple way to describe (some) XML documents
– Get attributes and subelements as Python attributes
– Parses string or stream into tree in memory
– Renders to string or stream
– Not (yet) released, developed for Elemental Security
• Conway's Game of Life / Damian's cellular automata
July 29, 2004
© 2004 Guido van Rossum
27
July 29, 2004
© 2004 Guido van Rossum
28
Question Time