Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'yet.': 0.04; 'argument': 0.05; 'skip:` 10': 0.07; 'bits': 0.09; 'constructor': 0.09; 'f.close()': 0.09; 'function,': 0.09; 'api': 0.11; 'cc:addr :python-list': 0.11; 'python': 0.11; '*you*': 0.16; 'file.read()': 0.16; 'foo()': 0.16; 'inclined': 0.16; 'personally,': 0.16; 'underlying': 0.16; 'valueerror': 0.16; 'wayne': 0.16; 'wrote:': 0.18; 'do.': 0.18; "hasn't": 0.19; 'passing': 0.19; 'seems': 0.21; '>>>': 0.22; 'input': 0.22; 'cc:addr:python.org': 0.22; 'header :User-Agent:1': 0.23; 'greg': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'designer': 0.30; 'enabled': 0.31; 'purely': 0.31; 'raised': 0.31; 'with,': 0.31; 'file': 0.32; 'class': 0.32; '(i.e.': 0.33; 'fri,': 0.33; 'agree': 0.35; 'but': 0.35; 'done,': 0.36; 'transition': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'being': 0.38; 'depends': 0.38; 'stable': 0.38; 'files': 0.38; 'does': 0.39; "couldn't": 0.39; 'called': 0.40; 'ensure': 0.60; 'remove': 0.60; 'easy': 0.60; "you're": 0.61; 'first': 0.61; 'happen': 0.63; 'more': 0.64; 'invalid': 0.68; 'results': 0.69; '"real': 0.84; 'blow': 0.84; 'stronger': 0.84; 'subject:skip:o 10': 0.84; '2013,': 0.91; 'state.': 0.95 Date: Mon, 13 May 2013 08:15:07 -0500 (CDT) From: Wayne Werner X-X-Sender: wayne@gilgamesh To: Greg Ewing Subject: Re: object.enable() anti-pattern In-Reply-To: <51902332.8090301@canterbury.ac.nz> References: <518a123c$0$11094$c3e8da3@news.astraweb.com> <51902332.8090301@canterbury.ac.nz> User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368450946 news.xs4all.nl 15907 [2001:888:2000:d::a6]:45080 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45237 On Mon, 13 May 2013, Greg Ewing wrote: > Wayne Werner wrote: >> On Fri, 10 May 2013, Gregory Ewing wrote: >> >>> f = open("myfile.dat") >>> f.close() >>> data = f.read() >> >> To clarify - you don't want a class that has functions that need to be >> called in a certain order with *valid input* in order to not crash. >> >> Exactly what does happen - a ValueError is raised because you're(*) passing >> self into the file.read() function, and that input is invalid > > The same argument can be applied to: > > foo = Foo() > foo.do_something() > foo.enable() # should have done this first > > You're passing an invalid input to Foo.do_something, > namely a Foo that hasn't been enabled yet. That is the crux of the argument - as designer of the class *you* need to ensure that when your constructor is done, your class is in a stable state. And that every other state transition (with valid input) results in your class then being in a stable state. If anything, the stronger argument is that `file.close()` is not a well designed function because it leaves your object in an unstable state. Which I would be inclined to agree with, but I couldn't give you the answer for what makes it better. Because the answer is the best one you can get in computer science: It depends. The reason that it depends, is because it depends on what you want to do. Do you want a program that seems purely functional? Do you want a program that's easy to maintain? Do you want a program that more accurately models the "real world"? Personally, I think the file object API in Python is about as good as it can get - but that's because it's working with "physical" things (i.e. files - bits on a platter, or flash/SSD drive...) which necessarily have a temporal nature. And it's much less badness to blow up on a call to `read` than it is to remove the `read` function and die with a NameError when the underlying file is in a closed state. At least in my opinion ;) -W