Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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.006 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; 'wrapped': 0.09; 'def': 0.12; '"w")': 0.16; '>>': 0.16; '12:50': 0.16; 'dict': 0.16; 'doing,': 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; 'email addr:gmail.com>': 0.22; 'convenient': 0.24; 'instance,': 0.24; 'file.': 0.24; "i've": 0.25; 'source': 0.25; '>': 0.26; 'references': 0.26; 'required.': 0.27; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; '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; 'quite': 0.32; 'open': 0.33; '(including': 0.33; 'entirely': 0.33; 'monday,': 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; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'keyword': 0.36; 'marks': 0.36; 'object,': 0.36; 'wishes,': 0.36; 'done': 0.36; "didn't": 0.36; 'method': 0.36; "i'll": 0.36; 'similar': 0.36; 'turn': 0.37; 'skip:& 10': 0.38; 'solving': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'list,': 0.38; 'resource': 0.38; 'rather': 0.38; 'skip:& 20': 0.39; 'itself': 0.39; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 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; "'strong'": 0.84; '2014,': 0.84; 'cole': 0.84; 'collection.': 0.84; 'dict.': 0.84; "it'd": 0.84; 'that)': 0.84; 'usage.': 0.84 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:to :content-type; bh=jFtGR4/O1hzlmAeJA9Z+cFwheE2ODnV0hZr4N6Updpo=; b=Jz8LhvD4U8ylBKQX7GdFJKnbhZe8K0/thHcOUBdBGSiXY2tP8OyMm4Xil8N4QExVQC lw2LcDBe7tJrIMd8hXogCPD3zRE6uA3f6fmzp6vSbPUJukaVjAx7DiqbfHJB/kpJcal7 tenYUbDpd31l/wnUt6K3grf9GTWlL4HcO0y0T26rgP9w2ki6l3FKKEX4tdsmA6uNBoAI dWlrmGqc8q8MHCER404yBQXlgEqcy/yNMYf+f2Rgk/Xe8QAxvwNaS1nMshonn/4tMx86 M6uEZ7rZUcoEWvKE63jI5uOFCy1CHYHsaBVLYQUd+Klsy626Dzway/JX7pnJVwFYTNoC drKQ== MIME-Version: 1.0 X-Received: by 10.15.26.199 with SMTP id n47mr45983082eeu.30.1391457471412; Mon, 03 Feb 2014 11:57:51 -0800 (PST) In-Reply-To: References: <858utviwgs.fsf@benfinney.id.au> <52EC3C40.7080402@stoneleaf.us> <52ec84bc$0$29972$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 3 Feb 2014 19:57:51 +0000 Subject: Re: __init__ is the initialiser From: Nicholas Cole To: Python Content-Type: multipart/alternative; boundary=089e01681bfe4dc0ff04f185f016 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: 158 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391459160 news.xs4all.nl 2850 [2001:888:2000:d::a6]:55189 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65375 --089e01681bfe4dc0ff04f185f016 Content-Type: text/plain; charset=ISO-8859-1 On Monday, 3 February 2014, Chris Angelico wrote: > 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 > When I have had similar problems, I've wrapped the 'strong' reference to the thing I might need to destroy inside an object, and had the 'destroy' method delete it. I'll admit that it isn't quite as convenient as having something that would work for arbitrary objects at arbitrary moments, but it works almost exactly as you describe for the file object above, and it can be quite neat and tidy. Best wishes, N. --089e01681bfe4dc0ff04f185f016 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Monday, 3 February 2014, Chris Angelico <rosuav@gmail.com> wrote:
On Tue, Feb 4, 2014 at 12:50 AM, Nicholas Cole <n= icholas.cole@gmail.com> wrote:
>> There have been occasional times I've wanted an "explicit= destruction"
>> feature. Rather than the facetious exception I listed above, it= 9;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 a= s a
>> destructed/destroyed (terminology debates aside) object. With cust= om
>> 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 lo= okup
>> keyword to its output file, and then destroy output files to remov= e
>> 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? =A0I'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 =3D {}

def add_log_file(fn, *keywords):
=A0 =A0 f =3D open(fn, "w")
=A0 =A0 for kw in keywords: log_files[kw]=3Df

for line in process_me_line_generator():
=A0 =A0 kw =3D line.split()[0]
=A0 =A0 f =3D log_files.get(kw)
=A0 =A0 if not f: continue
=A0 =A0 f.write(line+"\n")
=A0 =A0 if 'quit' in line:
=A0 =A0 =A0 =A0 # Okay, let's now close this file.
=A0 =A0 =A0 =A0 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<= br> 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<= br> 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

When I have had similar problems, I've= wrapped the 'strong' reference to the thing I might need to destro= y=A0inside an object, and had the 'destroy' method delete it.=A0

I'll admit that it isn't quite as convenient=A0= as having something that would work for arbitrary objects at arbitrary mome= nts, but it works almost exactly as you describe for the file object above,= and it can be quite neat and tidy.

Best wishes,

N. =A0
=

--089e01681bfe4dc0ff04f185f016--