[Tutor] Searching through files for values

Alan Gauld alan.gauld at btinternet.com
Fri Aug 14 12:59:34 CEST 2015

On 14/08/15 05:07, Jason Brown wrote:

>> for file_list in filenames:
>>>     with open(file_list) as files:
>>>          for items in vals:
>>>              for line in files:

Others have commented on your choice of names.
I'll add one small general point.
Try to match the plurality of your names to the
nature of the object. Thus if it is a collection
of items use a plural name.

If it is a single object use a single name.

This has the effect that for loops would
normally look like:

for <single name> in <plural name>:

This makes no difference to python but it makes it a lot
easier for human readers - including you - to comprehend
what is going on and potentially spot errors.

Also your choice of file_list suggests it is a list object
but in fact it's not, its' a single file, so simply reversing
the name to list_file makes it clearer what the nature of
the object is (although see below re using type names).

Applying that to the snippet above it becomes:

for list_file in filenames:
     with open(list_file) as file:
         for item in vals:
             for line in file:

The final principle, is that you should try to name variable
after their purpose rather than their type. ie. describe the
content of the data not its type.

Using that principle file might be better named as data
or similar - better still what kind of data (dates,
widgets, names etc), but you don't tell us that...

And of course principles are just that. There will be cases
where ignoring them makes sense too.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list