Well, it depends on what you want to do and what your goal is.
If you have a function that looks something like this:
def some_function(file_thingy):
with open(file_thingy.filename, 'w') as f:
f.write("Icky Icky Icky Patang NeeeeWom!")
Then it makes things much more generic - as long as you pass an object in that has a .filename
attribute that is a string then your function will work. A more common example of this is when people talk about duck typing. If it looks like a duck, walks like a duck, and quacks like a duck, well then it's a duck!
So if you have the following function:
def do_duck_things(a_duck):
print(a_duck.appearance)
a_duck.waddle()
a_duck.quack()
print("It must be a duck!")
Then you could pass it an instance of:
class Duck:
def __init__(self):
self.appearance = "White, like the AFLAC duck"
def quack(self):
print("Quaaaaaack!")
def waddle(self):
print("The duck waddles.")
or an instance of either of these classes:
class UglyDuckling:
def __init__(self):
self.appearance = "Suspiciously like a baby goose"
def waddle(self):
print("The ugly duckling waddles a bit like a duck.")
def quack(self):
print("Hoooonk!")
class Human:
def __init__(self):
self.appearance = "Looks like a human in a duck costume"
def waddle(self):
print("Surprisingly, he waddles quite like a duck.")
def quack(self):
print("<A sound quite like Donald Duck would make>")
So in your case, it really depends on what your function should be doing. If all it's doing is reading the contents of a file, then you can (and probably should) just send it a filename. But if you want to do something like, say, check the file against your stored MD5, or set that MD5, then it's perfectly appropriate to pass in the object.
HTH