New to Python. For in loops curiosity

Discussion in 'Python' started by Leonardo Petry, May 14, 2014.

  1. Hi All,

    So I am starting with python and I have been working on some simple exercises.

    Here is something I found curious about python loops

    This loop run each character in a string

    def avoids(word,letters):
    flag = True
    for letter in letters:
    if(letter in word):
    flag = False
    return flag

    The loop below (at the bottom) runs each line of the file

    fin = open('wordplay.txt');
    user_input = raw_input('Enter some characters: ')
    count = 0
    for line in fin:
    word = line.strip()
    if(avoids(word, user_input)):
    count += 1;

    This is just too convenient.
    Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

    Any comment is greatly appreciate. Thanks
     
    Leonardo Petry, May 14, 2014
    #1
    1. Advertisements

  2. Leonardo Petry

    John Gordon Guest

    Your code is not treating the contents of wordplay.txt as one long string
    because 'for line in fin:' tells it to read line-by-line.

    If you want to read the entire contents, use the read() method:

    file_content = fin.read()
     
    John Gordon, May 14, 2014
    #2
    1. Advertisements

  3. Leonardo Petry

    Rustom Mody Guest

    Rustom Mody, May 14, 2014
    #3
  4. Leonardo Petry

    Ian Kelly Guest

    Because the iterator for file-like objects iterates over lines, not characters.
     
    Ian Kelly, May 14, 2014
    #4
  5. Leonardo Petry

    Roy Smith Guest

    Because whoever designed the original file object decided that the right
    way to iterate over a file is line by line. In Python (although, at
    this level of explanation, I could be describing pretty much any
    language which has iterators), there is an iterator protocol which
    implements two ideas:

    1) There's a method to call to get the next item.

    2) There's a way for that method to signal that you've reached the end.

    Exactly what "the next item" means is up to whoever implements the
    iterator. In this case, it was decided that the most convenient thing
    would be for "item" to mean "line". If you really want to iterate over
    a file character-by-character, it's easy enough to write an adapter.
    Something like this (untested):

    def getchar(f):
    for line in f:
    for c in line:
    yield c

    Of course, if the native file iterator was character-by-character, then
    if you wanted it line-by-line, you would have to write the inverse, a
    function which accumulates characters until it sees a newline, and then
    returns that. Neither one is fundamentally better, or more correct than
    the other. One may just be more convenient for a particular use case.
     
    Roy Smith, May 14, 2014
    #5
  6. Every class can decide for itself how it will behave when iterated over
    (including deciding whether it can be iterated at all). File objects
    produce lines, strings produce characters, lists produce elements,
    dictionaries produce keys. Other objects do more exotic things.

    You might find this helpful: http://bit.ly/pyiter It's a PyCon talk
    all about iteration in Python, aimed at new learners.
     
    Ned Batchelder, May 14, 2014
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.