[Tutor] OT: Python 2.5 (Was Re: Length of longest item in a list, using a list comp)
wesley chun
wescpy at gmail.com
Fri Dec 29 07:26:42 CET 2006
> What are the must-have new features? (I could read the what's-new
> doc but that tells me about stuff I may not see the value in!)
>
> What are the features people are actually using regularly and find
> an improvement?
below is a quick summary of the 2.5 highlights. of these, i like/use
the following:
1, 2, 4, 5, 6, 7, (8), 9a, 9b. of course, my workplace is still on
2.3, and i've been "dying" to use generator expressions (2.4),
conditional expressions, and executing modules as scripts (easier to
run pdb directly on a module).
Python 2.5 Summary
=================
1) Conditional Expressions/Ternary Operators (PEP 308) -- finally! :-)
C ? T : F --> (C and [T] or [F])[0] --> T if C else F
2) Partial Function Application (PEP 309) -- adds to Python support
for currying and generalized partial function application:
from...
from operator import add
def add2(x):
return add(2, x)
to...
from operator import add
from functools import partial
add2 = partial(add, 2)
demo... (this is a short example of what it does... anyone have a more
*useful* example?)
>>> from operator import add
>>> def add2(x): return add(2, x)
>>> add2(5)
7
>>> from functools import partial
>>> add2b = partial(add, 2)
>>> add2b(5)
7
3) Absolute and Relative Imports (PEP 328) -- meant to address
intra-package import problems, esp. with std lib module name conflicts
4) Executing Modules as Scripts (PEP 338) -- new -m option very useful
for executing std lib modules as scripts
5) Unified try/except/finally (PEP 341) -- probably will be used a lot
since it turns a multi-level try-try-except-finally block into a
single-level try-except-finally block
6) The 'with' statement (PEP 343) - somewhat related to just above...
simplifies try-except-finally even further but "subsuming" the finally
clause (see context mgrs below) so you need just the single with
clause (and try-except if nec.). popular uses:
- need to have an open file (open it now and to close it later
automagically)
- need to acquire a lock (now and release it later automagically)
- need to obtain a database pool cxn (now and release it later
automagically)
7) Enhanced Generator Features (PEP 342) -- now you can "talk-back" to
a generator, meaning you can send data into it as you resume it...
.next() sends None, .send(obj) sends obj, .throw(exc) throws exception
exc
8) Exceptions as New-Style Classes -- all exceptions converted to new classes
9) new modules:
a) sqlite3 - probly used heavily due to interest in this lightweight RDBMS
b) ElementTree - probly used even *more* heavily due to interest
in this lightweight XML DOMish processor
c) ctypes - useful for those that want to interface directly to a
.so or .dll without writing extension wrappers
d) wsgiref - some reference tools for the new WSGI standard (for web apps)
e) hashlib - replaces most encryption modules
f) contextlib - used to write "context mgrs" for use with the 'with' stmt
for more demos and information about many of these features, check out
"Core Python Programming." :-)
cheers,
-wesley
--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
More information about the Tutor
mailing list