Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; '*not*': 0.07; 'affected': 0.07; 'context': 0.07; 'f.close()': 0.09; 'function,': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'valueerror:': 0.09; 'works.': 0.09; 'attributes,': 0.16; 'binding.': 0.16; 'file.read()': 0.16; 'mode,': 0.16; 'optional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:` 30': 0.16; 'statement.': 0.16; 'valueerror': 0.16; 'wayne': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'seems': 0.21; '(the': 0.22; '>>>': 0.22; 'input': 0.22; 'header:User-Agent:1': 0.23; 'skip:` 20': 0.24; 'visible': 0.24; 'file.': 0.24; 'certain': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'external': 0.29; 'raised': 0.31; 'file': 0.32; 'class': 0.32; 'checked': 0.32; 'fri,': 0.33; 'totally': 0.33; 'operations': 0.35; 'should': 0.36; 'manager': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'deleting': 0.60; 'new': 0.61; 'received:173': 0.61; 'name': 0.63; 'happen': 0.63; 'kept': 0.65; 'effectively': 0.66; 'invalid': 0.68; 'statement,': 0.68; 'idiom': 0.84; 'partially': 0.84; 'received:fios.verizon.net': 0.84; 'subject:skip:o 10': 0.84; '2013,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Jan Reedy Subject: Re: object.enable() anti-pattern Date: Sun, 12 May 2013 16:03:39 -0400 References: <518a123c$0$11094$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368389026 news.xs4all.nl 15868 [2001:888:2000:d::a6]:60791 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45199 On 5/12/2013 1:14 PM, Wayne Werner wrote: > On Fri, 10 May 2013, Gregory Ewing wrote: > >> Wayne Werner wrote: >>> You don't ever want a class that has functions that need to be called >>> in a certain order to *not* crash. >> >> That seems like an overly broad statement. What >> do you think the following should do? >> >> 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 > input - specifically: > > ValueError: I/O operation on closed file > > *where you actually means python, because when you call > `your_instance.method()`, it works effectively like a call to > `YourClass.method(your_instance)` The new idiom with open("myfile.dat") as f: data = f.read() partially encapsulates operations on the opened file before the automatic close. It is true however, that after the with statement, f in still uselessly bound to the closed file. (Well, you can check the mode, encoding, and other attributes, so not totally useless.) To completely encapsulate, one can end the block with 'del f'. I checked and this works. Since the external 'as x' name binding is optional, an internal reference to the context manager (the io.xxx instance) is kept in order to call the __exit__ method, and that is not affected by deleting the optional visible binding. tjr