Re:First Impressions of Python from a Java Developer
by elblanco on Monday November 08, 2010 @04:58PM
(#1884197)
I never really understood how the developers and maintainers of the Java language could put up with the overly complicated disaster that is file I/O. They've managed to take something that's relatively simple in most languages and turn it into an utter disaster. I was shocked, years ago, when I was learning Java after having lived in C++ world.
Re:First Impressions of Python from a Java Developer
by Luyt on Monday November 08, 2010 @04:22PM
(#1884066)
...and if you're processing text files line by line, it isn't even necessary to call read() or readlines() on the file, since a file object supports iteration. You could say:
for line in open("todo.txt"):
# do something with line
Re:First Impressions of Python from a Java Developer
by rbanffy on Monday November 08, 2010 @05:44PM
(#1884368)
You could also
[ do_something_to(line) for line in open('todo.txt') ]
Re:First Impressions of Python from a Java Developer
by xiongchiamiov on Monday November 08, 2010 @06:52PM
(#1884578)
Although you'd probably want to replace those square brackets with parens to make it a generator, if you're dealing with a largish file.
Re:First Impressions of Python from a Java Developer
by rbanffy on Monday November 08, 2010 @07:36PM
(#1884755)
Indeed. Hope it's not a huge file.
Re:First Impressions of Python from a Java Developer
by ShardPhoenix on Monday November 08, 2010 @05:13PM
(#1884253)
Default Java File I/O is pretty horrible but like a lot of things about Java, the widely-used Apache Commons makes it better:
String fileContents = FileUtils.readFileToString(new File("blah"));
Still not quite as concise as Ruby/Python/etc though.
Re:First Impressions of Python from a Java Developer
by vidar on Monday November 08, 2010 @02:12PM
(#1883554)
Surely late is better than never.
Re:First Impressions of Python from a Java Developer
by jbail on Monday November 08, 2010 @03:46PM
(#1883927)
I am admittedly late to the party. I went to a university where they taught Java (and unfortunately not Python). For the last 10 years, Java has paid the bills, and it still does, so I'm not knocking Java. I stuck with it for so long because I felt that it took that much real world usage to get a good grip on the language.
But you are right --- I'm definitely not the first developer to have had that "moment of zen" when opening files in Python, but hopefully I'm also not the last.
Re:First Impressions of Python from a Java Developer
by d0m on Monday November 08, 2010 @07:18PM
(#1884686)
So welcome :) In a couple of months, if you really want to have that "moment of zen" again, give a chance to SICP (it's a wonderful book) which will teach you functional programming and Scheme in the same time.
And I agree that it's funny for a Java programmer to be able to read a file with open('bleh').read() instead of that 15 stream buffer stream reader stream whatever line of code.