__init__ raises an exception, then __del__ will still be called
issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org/issue808164
3. Data model — Python 3.8.3rc1 documentation https://docs.python.org/3/reference/datamodel.html#object.__del__
Re: __init__ and __del__
Guido.van.Rossum@cwi.nl
Tue, 18 May 1993 16:32:23 +0200
- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: Guido.van.Rossum@cwi.nl: "Re: __init__ and __del__"
- Previous message: Chris Hoffmann: "__init__"
- In reply to: Chris Hoffmann: "__init__"
>From the responses so far I can conclude that having __init__
explicitly call the base class's __init__ is the best possible
solution, so let's move on to Chris' suggestion:
> By the way, will there be a __del__(self) method that is called at
> destruction time? My thought on this was that when python sees that an
> object should be garbage collected, it calls the object's __del__
> method (if any) before actually destroying it. Of course the
> interpreter would have to check the object's refcount after calling
> the function, as the method may have caused the object to be
> referenced by some other object.
The problem with this is, what to do if the __del__ call creates
another reference to the object? You can't delete it then since the
new reference would be dangling. But not deleting the object means
that the __del__ method may be called again later when the reference
count goes down to zero once again. Would this be a problem?
> I don't really have a good example of why you'd want this, other than
> for creating classes that keep track of how many instances of the
> class exist. Perhaps someone else can think of a good reason for
> having it. It just seems that if you have a function that is called at
> object creation that you should have one that is called at object
> destruction as well.
Actually, there are lots of situations where a class is used as a
wrapper around some "real-world" object (e.g. a window or a temporary
file or an audio device) that you would want to destroy (or restore to
a previous state) when the instance goes away. So yes, I think there
are many cases where this would be useful.
There's one problem with __del__, however: what if it raises an
exception? __del__ will be called implicitly from a DECREF(x)
statement in the C code, and I'm not going to add error checking to
all DECREF() statements. So these exceptions will have to be ignored.
In fact, there may already be an exception pending when DECREF() is
called, so it may have to save and restore the original exception.
Nasty!
One final thing to ponder: if we have a __del__ method, should the
interpreter guarantee that it is called when the program exits? (Like
C++, which guarantees that destructors of global variables are
called.) The only way to guarantee this is to go running around all
modules and delete all their variables. But this means that __del__
method cannot trust that any global variables it might want to use
still exist, since there is no way to know in what order variables are
to be deleted. Or is this not a useful feature?
--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
- Next message: Guido.van.Rossum@cwi.nl: "Re: __init__ and __del__"
- Previous message: Chris Hoffmann: "__init__"
- In reply to: Chris Hoffmann: "__init__"
Python Gotchas 1: __del__ is not the opposite of __init__ | Algorithm.co.il http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/
class A:
def __init__(self, x):
if x == 0:
raise Exception()
self.x = x
def __del__(self):
print('__del__')
print(self.x)
A(1)
A(0)
Python Archives (1993): Re: __init__ and __del__ https://legacy.python.org/search/hypermail/python-1993/0109.html
class A:
def __init__(self, x):
if x == 0:
raise Exception()
self.x = x
def __del__(self):
print('__del__')
print(self.x)
A(1)
A(0)
__init__ raises an exception, then __del__ will still be called的更多相关文章
- python中__init__ ,__del__ &__new__
__new__ __new__方法是用来创建对象,__new__方法需要有一个返回值,这个返回值表示创建出来的对象的引用 __init__ __init__方法在类的一个对象被建立时 ,马上执行.__ ...
- Python构造器及析构器:__init__与__new__及__del__
__init__与__new__这两个魔法方法组成了Python类对象的构造器,在Python类实例化时,其实最先调用的不是__init__而是__new__.__new__是负责实例化对象的,而__ ...
- nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
Deploying inside Eclipse v3.6 raises the exception. The WEB-INF/classes/ folder in the .war doesn't ...
- __del__()
__del__() 是类的内置函数,用于定义在脚本退出之前要执行的代码,因为有这个特性,通常被用来在脚本退出前关闭文件.关闭数据库连接.关闭网络连接等操作 [root@localhost ~]$ ca ...
- 描述符__get__,__set__,__delete__和析构方法__del__
描述符__get__,__set__,__delete__ 1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一 ...
- Python Exception处理
Python中的错误处理分为两类:语法错误和异常处理.语法错误一般是指由于python语句.表达式.函数等存在书写格式活语法规则上的错误抛出的异常,如python常见的缩进控制,若同层次的执行语句存在 ...
- C++ EH Exception(0xe06d7363)---捕获过程
书接上文<C++ EH Exception(0xe06d7363)----抛出过程>,下面我们讲下,VC++是如何catch到异常且处理的. 我们知道,在VC++里,C++异常实现的底层机 ...
- Python之路,Day8 - Python基础 面向对象高级进阶与socket基础
类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...
- 修改torndb库为依赖pymysql,使其适应python3,一个更简单的操作数据库的类。
1.python的MySQLdb和pymysql是两个基本数据库操作包,MySQLdb安装很麻烦,要有c++相关环境,python3也安装不了. python3一般安装pymysql,此包与MySQL ...
随机推荐
- [每日一题]ES6中为什么要使用Symbol?
关注「松宝写代码」,精选好文,每日面试题 加入我们一起学习,day day up 作者:saucxs | songEagle 来源:原创 一.前言 2020.12.23日刚立的flag,每日一题,题目 ...
- 红黑树规则,TreeSet原理,HashSet特点,什么是哈希值,HashSet底层原理,Map集合特点,Map集合遍历方法
==学习目标== 1.能够了解红黑树 2.能够掌握HashSet集合的特点以及使用(特点以及使用,哈希表数据结构) 3.能够掌握Map集合的特点以及使用(特点,常见方法,Map集合的遍历) 4.能够掌 ...
- Javascript 获得数组中相同或不同的数组元素
Javascript 获得数组中相同或不同的数组元素 在Javascript中,偶尔会用到获取数组中相同或不同的元素值的情况,以下提供了获得数组中相同或不同的 元素函数供参考学习使用. // 数字类型 ...
- MongoDb学习三(spring-data-mongodb)
本文采用2个种配置方式.xml配置 代码配置方式进行数据库的连接.实现简单的增删该查等一些操作.代码都有注释官方文档如下https://docs.spring.io/spring-data/mongo ...
- .NET 云原生架构师训练营(模块二 基础巩固 EF Core 更新和迁移)--学习笔记
2.4.6 EF Core -- 更新 状态 自动变更检测 不查询删除和更新 并发 状态 Entity State Property State Entity State Added 添加 Uncha ...
- Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释(转)
一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml dao.xml 2)解决:在web.xml中 <init-param> <param-nam ...
- SpringBoot整合sa-token,完成网站权限验证
sa-token是什么? sa-token是一个JavaWeb轻量级权限认证框架,其API调用非常简单,有多简单呢?以登录验证为例,你只需要: // 在登录时写入当前会话的账号id StpUtil.s ...
- kafka如何保证消息得顺序性
1. 问题 比如说我们建了一个 topic,有三个 partition.生产者在写的时候,其实可以指定一个 key,比如说我们指定了某个订单 id 作为 key,那么这个订单相关的数据,一定会被分发到 ...
- hive优化之小文件合并
文件数目过多,会给HDFS带来压力,并且会影响处理效率,可以通过合并Map和Reduce的结果文件来消除这样的影响: set hive.merge.mapfiles = true ##在 map on ...
- Java开发手册之设计规约
1.谨慎使用继承的方式来进行扩展,优先使用聚合/组合的方式来实现.说明:不得已使用继承的话,必须符合里氏代换原则,此原则说父类能够出现的地方子类一定能够出现,比如,"把钱交出来", ...