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


Groups > comp.lang.python > #104181

Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls.
Date Mon, 7 Mar 2016 14:38:36 +1100
Lines 44
Message-ID <mailman.4.1457321920.10335.python-list@python.org> (permalink)
References <nbiscc$v96$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de bpsbScBU7hcd0WpMQTOO2gWKIYVxxla3iWXX0mNCDltg==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'calls.': 0.07; 'cleanup': 0.07; 'cc:addr:python-list': 0.09; 'called.': 0.09; 'cleaned': 0.09; 'exception,': 0.09; 'def': 0.13; 'interpreter': 0.15; 'subject: \n ': 0.15; '2016': 0.16; '__del__': 0.16; 'called,': 0.16; 'finicky': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'magic': 0.16; 'nameerror': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reclaim': 0.16; 'subject:when': 0.16; 'systemexit': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'circular': 0.18; 'creates': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'prevent': 0.20; 'level,': 0.22; 'own.': 0.22; 'parameter': 0.22; 'trying': 0.22; 'code.': 0.23; 'bit': 0.23; 'references': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'module': 0.25; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; 'function': 0.28; 'this.': 0.28; 'termination': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'certainly': 0.30; 'waste': 0.30; 'rules': 0.31; "can't": 0.32; 'skip:_ 10': 0.32; 'run': 0.33; 'usually': 0.33; 'effort.': 0.33; 'foo': 0.33; 'received:google.com': 0.35; 'ahead': 0.35; 'subject:use': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'stuff': 0.38; 'why': 0.39; 'sure': 0.39; 'resources': 0.39; 'where': 0.40; 'no.': 0.62; 'total': 0.62; 'more': 0.63; 'mar': 0.65; 'inherit': 0.66; 'here': 0.66; 'special': 0.73; 'calls,': 0.84; 'chrisa': 0.84; 'cycles.': 0.84; 'subject:you': 0.85; 'to:none': 0.91; "'don't": 0.91; 'why?': 0.91
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; bh=KsNAKUE8Qbtl5OtQp4Bs51kYHYCm1GThT/E1QYFMg0I=; b=Bv/7UvpPfp4RZRSzdKMJo8iOBs3BbSbrCZfox6uZYzP6AzojRIfMWO1dd2F7i7xlXs XdYmOCkbLIdCTbvG9wEyd+mEYBn+Edrd9wuiSrtvK+lmMCQadrcmKgDc0uKnlo/wlYAG +wocWL1D2cZ+4g5qDyNkTUVNc//lcXvo0mjc2p86LCGOuIPkn0wty5VM41jqZbEW0+7V UkJb0KGH6KNlOQfJg7FUabPawAbmmyJ3CRv1LxE3BvsI9YdByiSZli/C5weg6edMU4Lv fKdmPY2vQMlchxaSsAsaGbCKmjE04Zfb9Upk4pwi62OWd3xRsRK0n4vrzj0yr1+nnnRb RXSA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=KsNAKUE8Qbtl5OtQp4Bs51kYHYCm1GThT/E1QYFMg0I=; b=jkLAbjAq/YQN6vYivb3qXUq++rdw7e4Zqsb1+vtVIDu62aKVqvzIfJpobOYLoV0BUR EFeywIO5qrq1ywHZv6ViD9Pu1m7boh9pPZQWsYTlRuvQRRGDUfCYKq4BtMTyOV1UOLrM xkWmpVcefiV/NKXwihbUH/UnRtgEA7Wnnb+FSmAh8FcweRCMcTBfus6IcXf5MnjGoujd cmsD5O2Y2gIUAHbr6O6qCskc1DrgKkRTGEIfNdF3s1PrszHJJ0HzwL6Nl5OTRLMiYCts 1c0xl1MSMOriZktw9krHTLfJAQKzFBTvernvoIB3qPAxkAeJzu1/y36UeMyFWNbQGmNq Z5Zw==
X-Gm-Message-State AD7BkJJxjEBlOWyZ02KoxKvZfLZG9//gsJnUiJTsyzTQti97zLi7aRn5LpCoRgbNBRkQdUg0ox1+RXLKl02ifA==
X-Received by 10.50.57.11 with SMTP id e11mr9749274igq.13.1457321916947; Sun, 06 Mar 2016 19:38:36 -0800 (PST)
In-Reply-To <nbiscc$v96$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104181

Show key headers only | View raw


On Mon, Mar 7, 2016 at 2:27 PM, Veek. M <vek.m1234@gmail.com> wrote:
> 1. What are the rules for using __del__ besides: 'don't use it'.

Use it as a last-shot cleanup of resources you own. Don't depend on
it, but use it.

> 2. What happens when I SystemExit? __del__ and gc are not invoked when I
> SystemExit and there's a circular reference - but why? The OS is going
> to reclaim the memory anyways so why be finicky about circular
> references - why can't we go ahead and call __dell_ and run gc?

SystemExit is an exception, same as any other (albeit one that doesn't
inherit from Exception, so it's usually left uncaught); the assumption
is that all resources will be cleaned up on process termination
anyway, so there's no need to run the GC to clean up reference cycles.
It's a total waste of effort.

> 3.
> import foo
> def __del__(self, foo=foo):
>   foo.bar()
>
> What happens here to prevent a NameError? Apparently without the foo=foo
> a NameError can occur? But why? import foo creates a reference to the
> object anyways so it's refcount will be above 0 anyways till __del__ is
> called.

I'm not sure where you're trying to put that code. If you put that at
module level, __del__ won't get called, because there's nothing
special about that name in a module.

During interpreter shutdown, modules will get unloaded, and stuff in
modules will get unloaded. But I'm not even going to _start_
explaining exactly what happens, until I know where this is heading;
there's almost certainly a better way to do this. What is it you're
actually trying to accomplish?

> 4. also, are method calls more efficient than function calls?

No. They in fact *are* function calls, but with a slight bit of magic
so they get their first parameter passed in. You can consider them to
be identical to function calls.

ChrisA

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


Thread

__del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. "Veek. M" <vek.m1234@gmail.com> - 2016-03-07 08:57 +0530
  Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. Chris Angelico <rosuav@gmail.com> - 2016-03-07 14:38 +1100
  Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. Ben Finney <ben+python@benfinney.id.au> - 2016-03-07 14:42 +1100
  Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-07 16:56 +1100
  Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. "Veek. M" <vek.m1234@gmail.com> - 2016-03-07 11:43 +0530
    Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-07 19:37 +1100
      Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls. "Veek. M" <vek.m1234@gmail.com> - 2016-03-07 16:56 +0530

csiph-web