Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65352

Re: __init__ is the initialiser

References (8 earlier) <bl81skFh14iU1@mid.individual.net> <mailman.6313.1391383680.18130.python-list@python.org> <roy-C454F0.18405902022014@news.panix.com> <CAPTjJmo6qpfP74YF+2xKV1y-qUNt6rG+8kHkoYJZXbb3HED5pg@mail.gmail.com> <CAAu18hevvTDWizx8Gt2S4MUv04KQ0uw7sZaOk-pi-ffsOtG8nw@mail.gmail.com>
Date 2014-02-04 04:57 +1100
Subject Re: __init__ is the initialiser
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6349.1391450237.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Feb 4, 2014 at 12:50 AM, Nicholas Cole <nicholas.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'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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: __init__ is the initialiser Terry Reedy <tjreedy@udel.edu> - 2014-01-31 22:16 -0500
  Re: __init__ is the initialiser Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-01 05:23 +0000
    Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-01 00:25 -0500
      Re: __init__ is the initialiser Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-03 12:15 +1300
        Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-03 10:27 +1100
          Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-02 18:40 -0500
            Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-03 11:07 +1100
            Re: __init__ is the initialiser Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-02-02 17:24 -0800
            Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-03 12:37 +1100
            Re: __init__ is the initialiser Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-02-02 17:54 -0800
            Re: __init__ is the initialiser Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-03 08:44 -0500
            Re: __init__ is the initialiser Nicholas Cole <nicholas.cole@gmail.com> - 2014-02-03 13:50 +0000
            Re: __init__ is the initialiser Ian Kelly <ian.g.kelly@gmail.com> - 2014-02-03 10:44 -0700
            Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-04 04:57 +1100
            Re: __init__ is the initialiser Nicholas Cole <nicholas.cole@gmail.com> - 2014-02-03 19:57 +0000
            Re: __init__ is the initialiser Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-03 19:38 -0500
        Re: __init__ is the initialiser Dave Angel <davea@davea.name> - 2014-02-02 19:35 -0500
          Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-02 19:45 -0500
            Re: __init__ is the initialiser Dave Angel <davea@davea.name> - 2014-02-02 22:14 -0500
            Re: __init__ is the initialiser Skip Montanaro <skip@pobox.com> - 2014-02-02 21:15 -0600
            Re: __init__ is the initialiser Dave Angel <davea@davea.name> - 2014-02-03 00:06 -0500
              Re: __init__ is the initialiser Roy Smith <roy@panix.com> - 2014-02-03 00:12 -0500
                Re: __init__ is the initialiser Chris Angelico <rosuav@gmail.com> - 2014-02-03 16:49 +1100
                Re: __init__ is the initialiser Ethan Furman <ethan@stoneleaf.us> - 2014-02-02 21:34 -0800
    Re: __init__ is the initialiser Rustom Mody <rustompmody@gmail.com> - 2014-01-31 21:31 -0800

csiph-web