Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104181
| 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 | 2016-03-07 14:38 +1100 |
| Message-ID | <mailman.4.1457321920.10335.python-list@python.org> (permalink) |
| References | <nbiscc$v96$1@dont-email.me> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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