New to Python. For in loops curiosity

L

Leonardo Petry

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
 
J

John Gordon

In said:
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

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()
 
I

Ian Kelly

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?

Because the iterator for file-like objects iterates over lines, not characters.
 
R

Roy Smith

Leonardo Petry said:
Basically my question is: Why is python not treating the contents of
[a file] as one long string and looping each character?

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.
 
N

Ned Batchelder

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

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top