Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2006, 5:16 AM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile Python 2.5

Some interesting stuff has been added.

If you are interested in the changes, got to:
http://docs.python.org/dev/whatsnew/whatsnew25.html
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 15th, 2006, 9:56 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
This is one part they added: http://docs.python.org/dev/whatsnew/node3.html

x = true_value if condition else false_value

And what was wrong with:
x = (true_value, false_value)[x==False]

Geez...
Sane is offline   Reply With Quote
Old Apr 16th, 2006, 7:59 AM   #3
Kakao
Newbie
 
Join Date: Feb 2006
Posts: 9
Rep Power: 0 Kakao is on a distinguished road
Quote:
Originally Posted by Sane
This is one part they added: http://docs.python.org/dev/whatsnew/node3.html

x = true_value if condition else false_value

And what was wrong with:
x = (true_value, false_value)[x==False]

Geez...
Yes, an indexed tuple is very convenient as well. But note that a true condition returns the second tuple element and not the first as would be more natural:

x = (a, b)[True] # x = b
x = (a, b)[False] # x = a

The proposed sintax address it making the true value come first. In your example you had to do a NOT(condition) to make it work the same.
Kakao is offline   Reply With Quote
Old Apr 16th, 2006, 10:38 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by Kakao
The proposed sintax address it making the true value come first. In your example you had to do a NOT(condition) to make it work the same.
Not quite. I've noticed Python can be quite finicky about that. I don't have any examples, but I've remembered times when certain objects/items have been considered "not False" but not "True". Therefore we must use "not False" to withstand all the possible cases.
Sane is offline   Reply With Quote
Old Apr 15th, 2006, 10:00 AM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
A few things are clearly taken from Ruby.

Especially this: http://docs.python.org/dev/whatsnew/node10.html

Which are basically copying Ruby blocks:

Ruby
File.open("test.txt", 'r') do |file|
    #do stuff with file
    #automatically closed
end

Python in 2.5
with open("test.txt", 'r') as f:
    for line in f:
        print line    
 
   #do stuff with file
   #automatically closed

But Ruby has copied plenty of things from Python, so it is not necessarily a bad thing. Just interesting...
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 15th, 2006, 10:41 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Jessehk
Which are basically copying Ruby blocks
No, they're not. Although they can be used in similar ways, Python's with statement is different in implementation.

For instance, the file open code you had in Ruby, would translate to this in Python:
def file_stuff(file):
    #do stuff with file
    #automatically closed
ruby_style_open("test.txt", 'r', file_stuff)
Whilst the Python with statement translates so:
file = open("test.txt", 'r')
file.__enter__()
#do stuff with file
#automatically closed
file.__exit__()
See the difference?
Quote:
Originally Posted by Sane
They also added any() and all(). I still stick with my opinion that it's not only redundant, but slower...
Uh... Why would it be slower? Presumably, any and all are just wrappers around "True in" and "False not in".

I kind of like these wrappers. In my opinion, they make things a little neater and easier to read:
if False not in (x < 10 for x in numbers):
    print "All numbers below ten"

if all(x < 10 for x in number):
    print "All numbers below ten"
Personally, I'm all looking forward to Python's continuation support
Arevos is offline   Reply With Quote
Old Apr 15th, 2006, 10:04 AM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,086
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
The net result of the 2.5 optimizations is that Python 2.5 runs the pystone benchmark around XXX% faster than Python 2.4.
Lame... T__T


They also added any() and all(). I still stick with my opinion that it's not only redundant, but slower...

# 2.4 "any"
if True in (0, 1, 2, 3, 4):
    print "Any"

# 2.5 "any"
if any((0, 1, 2, 3, 4)):
    print "Any"

# 2.4 "all"
if not False in (1, 2, 3, 4, 5):
    print "All"

# 2.5 "all"
if all((1, 2, 3, 4, 5)):
    print "All"
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC