Why no open(f, "w").write()?
Gustavo Cordova
gcordova at hebmex.com
Thu May 30 13:21:46 EDT 2002
>
> Of course, if write() returned the file object instead of nothing,
> one could use something like:
>
> open(f, "w").write(data).close()
>
> but that might not be Pythonic enough (or too C++-y) for some.
>
> Gary Duzan
> BBN Technologies
> A Verizon Company
>
Well, subclassing is your friend!
-- snip --
class File(file):
def write(self, data):
file.write(self, data)
return self
-- snip --
So now you can have your cake and eat it too. :-)
Or, a "quickfile" kind of thing:
-- snip --
def WriteFile(filename, mode="w", *data):
f = file(filename,mode)
for item in data: f.write(item)
f.close()
-- snip --
ta-daa. Trivial problems demand trivial solutions.
-gustavo
More information about the Python-list
mailing list