Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'lines,': 0.07; 'none,': 0.07; 'suppose': 0.07; 'tool,': 0.07; '"if': 0.09; 'feature.': 0.09; 'issue:': 0.09; 'line:': 0.09; 'lookup': 0.09; 'references.': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"w")': 0.16; '12:50': 0.16; 'dict': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garbage': 0.16; 'illustrate': 0.16; 'types,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'file,': 0.19; 'work,': 0.20; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'source': 0.25; 'references': 0.26; 'required.': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'related': 0.29; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; 'everywhere': 0.31; 'object.': 0.31; 'time;': 0.31; 'file': 0.32; 'open': 0.33; '(including': 0.33; 'entirely': 0.33; 'there,': 0.34; 'skip:d 20': 0.34; 'subject:the': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'connection': 0.35; 'problem.': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'keyword': 0.36; 'marks': 0.36; 'object,': 0.36; 'done': 0.36; "didn't": 0.36; "i'll": 0.36; 'similar': 0.36; 'turn': 0.37; 'solving': 0.38; 'files': 0.38; 'list,': 0.38; 'resource': 0.38; 'rather': 0.38; 'itself': 0.39; 'sure': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'remove': 0.60; 'easy': 0.60; 'above,': 0.60; 'times': 0.62; 'offer': 0.62; 'kind': 0.63; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'close': 0.67; 'special': 0.74; 'cole': 0.84; 'collection.': 0.84; 'dict.': 0.84; "it'd": 0.84; 'that)': 0.84; 'usage.': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=a/v4dUFgGEf+RFnxFKwFScl1EPSZqGcy3R2NrLQpnms=; b=Nv9SmFjtLer8zb363KlYXR7vPc59RTOT9sIq/AFQevKAlCX6glGyUEt1vJlU/ubmmH garM2Ni4g00DzS0L4y+QeaaK8ZVOe208rObF+NFzRzbET9f8HpuMP7rlxBv26dds2uAR FP3EpM9kJh+3WKfYt07bg3amAMNR9W+PvqAj7+w5jFni2/m+U29rga2y6xq3SPtOdjaJ tUMqxSToLPDw/xVdymHNi1mWhnEJValT7vOuyhh7u2fE0ZsrqS7V8tFDfxsq4yStW2ZA a9K5q3Krr6mZq6pwC1rMWJI5FWYaUIX03/ulNw+RbERsXOjAgRVf9MyJRATyyYYVUfao pvYg== MIME-Version: 1.0 X-Received: by 10.68.201.10 with SMTP id jw10mr38890429pbc.25.1391450233440; Mon, 03 Feb 2014 09:57:13 -0800 (PST) In-Reply-To: References: <858utviwgs.fsf@benfinney.id.au> <52EC3C40.7080402@stoneleaf.us> <52ec84bc$0$29972$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 4 Feb 2014 04:57:13 +1100 Subject: Re: __init__ is the initialiser From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391450237 news.xs4all.nl 2891 [2001:888:2000:d::a6]:41214 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65352 On Tue, Feb 4, 2014 at 12:50 AM, Nicholas Cole wrote: >> There have been occasional times I've wanted an "explicit destruction" >> feature. Rather than the facetious exception I listed above, it'd be >> better to have all those references (including the original one in a, >> since there's nothing special about that) turn into some kind of "null >> state" - either None, or a special object that marks itself as a >> destructed/destroyed (terminology debates aside) object. With custom >> types, I can mark them off with a special flag, and check that all the >> time; but I can't, for instance, have a dict that maps some lookup >> keyword to its output file, and then destroy output files to remove >> all their references from everywhere in the dict. (I have had >> something along these lines, a bit more complicated than this, but not >> in Python.) > > Can't you get close to that using weakrefs? I'll admit that care is required. Weakrefs are a related tool, but solving a different problem. What I wanted here was an easy way to force all references to a particular file to be wiped out, based on one of the existing references. Here's a concocted setup that's broadly similar to what I was doing, which might illustrate the issue: log_files = {} def add_log_file(fn, *keywords): f = open(fn, "w") for kw in keywords: log_files[kw]=f for line in process_me_line_generator(): kw = line.split()[0] f = log_files.get(kw) if not f: continue f.write(line+"\n") if 'quit' in line: # Okay, let's now close this file. destruct(f) In this particular case, I could use "f.close()" and "if not f or f.closed: continue", but that requires that the object cooperate in this way, and I'm not entirely sure about resource usage. (I was actually working with a database connection object, IIRC, which didn't offer me a way to inquire if it was still open or not.) To do this with weak refs, I'd have to have some other source of strong refs, and closing would be done by digging through that list, disposing of it from there, and then triggering garbage collection. I suppose it could be made to work, but it feels like going about everything backwards. ChrisA