Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Stephen Hansen Newsgroups: comp.lang.python Subject: Re: A fun python CLI program for all to enjoy! Date: Fri, 06 May 2016 23:03:41 -0700 Lines: 56 Message-ID: References: <7e52b918-2087-f93f-43cb-3411c1cdc881@mrabarnett.plus.com> <572D28CF.3080100@stoneleaf.us> <1462601021.570549.600707729.5AB38312@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Wm4M32D/eB/A54M4Ru7otAdK8ZBCzSY+YD5tZR6bm9ng== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'preferred.': 0.04; 'cpython': 0.05; 'mrab': 0.05; 'counting': 0.07; 'defines': 0.07; 'f.close()': 0.07; 'adopted': 0.09; 'bindings': 0.09; 'closed,': 0.09; 'deletes': 0.09; 'received:internal': 0.09; 'references,': 0.09; 'through,': 0.09; 'python': 0.10; 'subject:python': 0.14; 'explicitly': 0.15; '(either': 0.16; 'closed:': 0.16; 'dfs': 0.16; 'improper': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'sequence:': 0.16; 'storing': 0.16; 'subject:program': 0.16; 'then...': 0.16; 'wrote:': 0.16; 'deleted.': 0.18; 'language': 0.19; 'issue.': 0.20; 'preferred': 0.20; "aren't": 0.22; 'explicit': 0.22; 'function,': 0.22; 'implicit': 0.22; 'object.': 0.22; 'stephen': 0.22; 'file.': 0.22; 'code.': 0.23; 'wrote': 0.23; 'references': 0.23; 'second': 0.24; 'somewhere': 0.24; 'header:In-Reply-To:1': 0.24; 'command': 0.26; 'required.': 0.26; 'fri,': 0.27; 'object,': 0.27; 'reaches': 0.27; 'said,': 0.27; 'sequence': 0.27; 'function': 0.28; 'accidentally': 0.29; 'itself,': 0.29; "i'm": 0.30; 'strongly': 0.30; 'checked': 0.31; 'operations': 0.31; 'probably': 0.31; 'statement': 0.32; 'subject:all': 0.32; 'open': 0.33; 'file': 0.34; 'nothing.': 0.35; 'something': 0.35; "isn't": 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'needed': 0.36; 'closing': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'done.': 0.37; 'thought': 0.37; 'seem': 0.37; 'things': 0.38; 'detail': 0.38; 'times.': 0.38; 'received:66': 0.38; 'feedback': 0.38; 'files': 0.38; 'end': 0.39; 'why': 0.39; 'sure': 0.39; 'does': 0.39; "didn't": 0.39; 'resources': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'behavior': 0.61; 'close': 0.61; 'skip:u 10': 0.61; 'header :Message-Id:1': 0.61; 'further': 0.62; 'you.': 0.64; 'else.': 0.66; "'with'": 0.84; '*new': 0.84; 'mechanics': 0.84; 'do:': 0.91; 'period.': 0.95 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=gZ/luVwLrVFoYRp9/ZHC+yav7EI=; b=p8L7jF CqiBYv2oPc+coDU5JQdBL6Z6RVz5AavyACkPwSoVy6n5raeoUHPXizTxJGz2XDjF fW2Uq43v+16Z76+SdGkfWM0tRcXx6ZfjhuxH5XJOAupdHhcFceGGTxciNe2SFaoC bLf+qlzbgO3eVYrRJw1jZ1JdGlQN5i6PmHzHo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=gZ/luVwLrVFoYRp 9/ZHC+yav7EI=; b=QwZcVTg5HxRgNqP0j5b2KsX6WaJRsMUNARtQVDZ9BmoEpVB jBKEjA0UL3GryNrBfClwJDBiIxIVT6NkXUIc/BvYqmAzndAZ/d1lqRXv8c6g93dO a3+FqNvlWrYJ2Xo4fBXUBPQ2PvWqIbYXSJ+giLVSjL04Nn6rtghYkJTWyqTU= X-Sasl-Enc: RVMqeuj5HNq79HCDgnsJcXlkAce/VTo9tOs09tmt+GeW 1462601021 X-Mailer: MessagingEngine.com Webmail Interface - ajax-140377c4 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1462601021.570549.600707729.5AB38312@webmail.messagingengine.com> X-Mailman-Original-References: <7e52b918-2087-f93f-43cb-3411c1cdc881@mrabarnett.plus.com> <572D28CF.3080100@stoneleaf.us> Xref: csiph.com comp.lang.python:108252 On Fri, May 6, 2016, at 04:58 PM, DFS wrote: > Improper f.close didn't seem to affect any of the files my program wrote > - and I checked a lot of them when I was writing the code. To be clear, its not an "improper" f.close. That command is simply not closing the file. Period. "f.close" is how you get the 'close' function from the 'f' object, and then... you do nothing with it. If you removed "f.close" entirely, you'd get the exact same behavior as you have now. The "f.close" does nothing. That said, in CPython semantics, closing a file explicitly is often not required. CPython is reference-counted. Once the references to an object reaches 0, CPython deletes the object. This is an implementation detail of the CPython and not a guarantee of the Python language itself, which is why explicit close calls are preferred. So while 'f.close' does nothing, CPython might be closing the file *anyways*, and it might work... but that 'might' is hard to reason about without a deeper understanding, so using explicit closing mechanics (either via f.close() or with or something else) is strongly recommended. For example, if you were to do: for item in sequence: f = open(item, 'wb') f.write("blah") It probably works fine. The first time through, 'f' is bound to a file object, and you write to it. The second time through, 'f' is bound to a *new file object*, and the original file object now has 0 references, so is automatically deleted. The last sequence through, f is not closed: the 'for loop' is not a scope which deletes its internal name bindings when its done. So that 'f' will likely remain open until the very end of the current function, which may be an issue for you. Implicit closing actually works in a large number of situations in CPython, but it isn't a good thing to rely on. It only works in simple operations where you aren't accidentally storing a reference somewhere else. You have to keep track of the references in your head to make sure things will get closed at proper times. The 'with' statement clearly defines when resources should be closed, so its preferred (As I see you've adopted from other responses). But its also needed in other Python implementations which might not follow CPython's reference counting scheme. I'm not giving further feedback because MRAB caught everything I thought was an issue. -- Stephen Hansen m e @ i x o k a i . i o