My Python annoyances

  • Thread starter =?iso-8859-1?B?QW5kcuk=?=
  • Start date
A

Alex Martelli

Steven D'Aprano said:
What do people think about functions that accept either a file name or a
file object?

def handle_file(obj):
if type(obj) == str:
need_to_close = True
obj = file(obj, 'r')
else:
need_to_close = False
do_something_with(obj.read())
if need_to_close:
data.close()

Good idea? Bad idea? Just a matter of personal preference?

Acceptable as an idea, but a disaster in terms of this specific
implementation (as coded, it would reject a Unicode string, or any other
string-like object, for example). Also, if all you're going to do with
the file is .read() it in one big gulp, there's no real advantage to
this approach, either.

Assuming the way you're going to use the file-like object is subtler
(e.g., loop line by line so that huge files can be processed without
overwhelming memory), then a better implementation may be warranted:

def handle_file(file_or_path):
try:
f = open(file_or_path)
finis = f.close
except TypeError:
f = file_or_path
def finis(): pass
try:
for line in f:
...
finally:
finis()

This version accepts anything that open is happy with, or else any
sequence of lines, including but not limited to a file or file-like
object open for reading. Now this, it seems to me, is a helpful
approach to polymorphism.


Alex
 

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
474,157
Messages
2,570,879
Members
47,414
Latest member
djangoframe

Latest Threads

Top