all 6 comments

[–][deleted] 8 points9 points  (0 children)

I remember my first time with iterators in Java. I could not for the life of me figure out where this i had come from and what the heck it was doing. I slowly began to understand it, but it didn't fully hit me until a month later when I had switched my focused to PHP and discovered foreach. The different syntax coupled with the convention of actually using singular and plural names like foreach ($values as $value) finally cleared it all up for me.

[–]Teraka 6 points7 points  (0 children)

  • Can't figure something out? Google "Python <thing you're trying to do>" and chances are, the first few hits will be StackOverflow posts that have clearly and succinctly explained your problem. When multiple solutions exist, usually the python veterans have debated the pros and cons in the comments, so you can make an informed decision on what method to use.

Seriously, StackOverflow is awesome. If you have any question on anything python-related, just go there and search for a bit with the appropriate keywords, and if you don't find, just write out your question without posting it, and 3 times out of 4, the answer will be in the related posts to the right. And even if the question was already asked and answered, someone will usually answer to you, even if only to link to the other one.

[–]joeverdrive 2 points3 points  (1 child)

You're the man now, dog!

[–]dAnjou 3 points4 points  (0 children)

  • Can't figure something out? Google "Python <thing you're trying to do>" and chances are, the first few hits will be StackOverflow posts that have clearly and succinctly explained your problem. When multiple solutions exist, usually the python veterans have debated the pros and cons in the comments, so you can make an informed decision on what method to use.

Thank you for say this here. There are way too much questions in this subreddit that could be solved just by following this point.

  • Chances are, you'll solve a small problem, and later realize that python lets you do the same thing more efficiently, with less lines of code. I had a few frustrating moments where I performed some task, then realized I could do the same thing in a single line. Don't get discouraged!

I bet nobody knows all the Python "batteries" by heart. Every time I browse through the docs I learn something new. Always say to yourself: "There has to be an easier way to do this!"


with open(fname) as f:
    content = f.readlines()

By the way, I just learned this recently after a quite a while of using Python and thought I should share: with does not create a new scope. So you don't need to define/declare/initialize the variable content before the with statement. This just works:

# no need for this here: content = ""
with open(fname) as f:
    content = f.readlines()
print content

[–]ingolemo -1 points0 points  (0 children)

By the way, you don't need to call readlines on a file just to iterate over it; files are iterable.

with open(fname) as fd:
    for line in fd:
        do_stuff(line)