Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!roy From: Roy Smith Newsgroups: comp.lang.python Subject: Re: Status of side-effecting functions in python Date: Sun, 26 Oct 2014 17:56:51 -0400 Organization: PANIX Public Access Internet and UNIX, NYC Lines: 34 Message-ID: References: <438d510f-05ae-47dd-bcda-42a8b777c99b@googlegroups.com> <683c84d8-d916-4b63-b4b2-92cd2763e260@googlegroups.com> NNTP-Posting-Host: localhost X-Trace: reader1.panix.com 1414360611 11499 127.0.0.1 (26 Oct 2014 21:56:51 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Sun, 26 Oct 2014 21:56:51 +0000 (UTC) User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Xref: csiph.com comp.lang.python:79730 In article <683c84d8-d916-4b63-b4b2-92cd2763e260@googlegroups.com>, wxjmfauth@gmail.com wrote: > Le dimanche 26 octobre 2014 14:41:43 UTC+1, Dan Sommers a écrit : > > On Sun, 26 Oct 2014 00:45:49 -0700, wxjmfauth wrote: > > > > > Ditto for .write(). Why should it return "something" ? > > > > > >>>> with open('z.txt', 'w') as f: > > > ... f.write('abc') > > > ... > > > 3 > > > > OTOH, why shouldn't it return something? In this case, it returns the > > length of the string written. This value is analogous to the value > > returned by the underlying OS function (at least on a POSIX-like system, > > where write(2) returns the number of bytes written). This value can be > > useful for detecting when things have gone wrong; e.g., disk full, > > network down, pipe broken, etc. Practicality definitely beats purity. > > > > At one time, on a huge project, millions of lines of C and assembly > > code, we had a local guideline *not* to write void functions. The idea > > was to return something that might be useful later, even if it seemed > > unlikely now. A simple success flag was sufficient; as functions grew, > > often did their failure modes. > > > > Dan > > Yes and no. If something goes wrong in a .write() method, > is not Python supposed to raise an error? (!) Define "wrong". It is not an error for a write() call to consume fewer bytes than were requested. How would you expect this to be handled in Python? Raise DataPartiallyWrittenError?