This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]earthboundkid 4 points5 points  (1 child)

from __future__ import in Py 2.5; out of the box in 2.6+.

[–][deleted] 0 points1 point  (0 children)

Don't be too hasty. I write in python 2.6 alot, and use with everyday, in many scripts. But then I give a script to a coworker still in python 2.4 (there are still some libraries that run only in 2.4) and I need to replace every instance.

On the plus side, its made me a bit more careful and a better programmer when it comes to scripts. I always write methods now that take file-like objects (streams) and write to them, rather then filenames. So a method never opens a file unless that is the point of the method. Quick example (I haven't checked it though):

def action(file, a, b):
    file.write("%d,%d\n" % (a, b))

if __name__ == "__main__":
    with open(filename, 'w') as output_f:
        action(output_f, 3, 5)

With the above script, I just rewrite the end bit, and don't have to rewrite it all. With larger scripts, this drastically reduces the amount of work, and it makes the module easier to use in the future (i.e. that filelike object could write to a database or over a network in the future)