类的专有方法(__del__)
# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#http://www.bubuko.com/infodetail-313791.html #类的专有方法(__del__)
'PS:析构函数只需要明白其用途,目前无需深入学习' #__init__
#作用:当一个类实例删除时被调用
#析构函数与构造函数相反,当对象脱离其作用域时(对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做清理善后的工作;开辟了一个内存空间之后,利用完毕了要释放内存
#__del__()也是可选的,如果不提供,python在后台会提供默认析构函数。
#如果要显式的调用析构函数,可以使用del关键字,如:del对象名
#析构函数是在生命周期里最后一个被调用的函数,所有删除、释放资源都常常会在这个函数内调用 class Auto():
def __init__(self,color):
self.__color=color
print '构造函数:',self.__color def __del__(self):#定义析够函数
self.__color="Red"
print self.__color
print "Release..." def getColor(self):
print self.__color if __name__=='__main__':
car=Auto('blue')
car.getColor()
#首先调用getColor方法,执行print self.__color得输出结果blue,
#如果在这里,所有的程序都执行完毕,将执行del析构函数,这时的self.__color应该是"Red"
del car #删除实例化car后,再次调用car.getColor()会报错,
#因为car没有进行实例化无法调用!!
#car.getColor()#NameError: name 'car' is not defined '''
构造函数: blue
blue
Red
Release...
'''
类的专有方法(__del__)的更多相关文章
- 4 python 类的专有方法介绍
1.__init__ : 构造函数,在生成对象时调用 该方法是在对象产生之后才会执行,只用来为对象进行初始化操作,可以有任意代码,但不一定有返回值. 所谓初始化构造函数就是在构造对象的同时被对象自动 ...
- 类的专有方法(__repr__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://blog.csdn.net/yyt8yyt8/article/details/7030416 ...
- [py]类的专有方法
陆陆续续总结一些用到的类的特殊方法 看源码总会看到一些奇奇怪怪的写法: 掺杂着设计模式 https://coding.net/u/RuoYun/p/Python-design-pattern/git/ ...
- 类的专有方法(__getattr__和__setattr__、__delattr__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.360doc.com/content/15/0413/19/12067640_4629 ...
- 类的专有方法(__getitem__和__setitem__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__getitem__和__s ...
- 类的专有方法(__len__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__len__) #如果一个类 ...
- 类的专有方法(__init__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.cnblogs.com/zyxstar2003/archive/2011/03/21/ ...
- 类的析构方法__del__
析构方法: 语法: class 类名: def __del__(self): ... 说明: 析构方法在对象被销毁时被自动调用 python建议不要在对象销毁时做任何事情,因为销毁的时间难以确定 cl ...
- python干货-类属性和方法,类的方法重写
类属性与方法 类的私有属性 __private_attrs: 两个下划线开头,表明为私有,外部不可用,内部使用时self.__private_attrs. 类的方法 在类的内部,使用 def 关键字来 ...
随机推荐
- Material Design(原质化设计)视觉设计语言规范 踏得网镜像
Android 5.0 Lollipop(棒棒糖,也就是之前的代称Android L)全面实践了谷歌最新研发的 Material Design 设计语言规范,只是该设计规范并不是仅针对移动平台. 我们 ...
- MFC中添加ToolTip提示框
PART 1 MFC 对话框中的 Buttton添加提示 例如我们想在一个对话框中的一个button控件添加tooltip,实现的方法如下: 1. 在该对话框的类中添加一个CToolTipCtrl类型 ...
- 算法:插入排序(Insertion Sort)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS获取网络类型的四种方法
Reachability类只能区分WIFI和WWAN类型,却无法区分2G网和3G网. 网上也有些方法,却都存在Bug. 经过网上查找资料和测试,基本上总结了以下几种方法: 1.使用导航栏的方式:(私有 ...
- 阿里春招Android面经
作者:淘萄桃 链接: https://www.jianshu.com/p/a07ccaad832d 本文由作者授权发布. 笔者参加18年阿里春招,有幸最终拿到阿里offer,base杭州,岗位客户端开 ...
- 利用Mircosoft URLRewriter.dll实现页面伪静态
一,获得Mircosoft URLRewriter.dll: 获得Mircosoft URLRewriter.dll可以到http://www.microsoft.com/china/msdn/lib ...
- CRF 及CRF++ 安装与解释
CRF简介 Conditional Random Field:条件随机场,一种机器学习技术(模型) CRF由John Lafferty最早用于NLP技术领域,其在NLP技术领域中主要用于文本标注,并有 ...
- OpenCV教程(41) 人脸特征检测
在OpenCV中,自带着Harr分类器人脸特征训练的文件,利用这些文件,我们可以很方面的进行人脸,眼睛,鼻子,表情等的检测. 人脸特征文件目录: ../opencv2.46/op ...
- longest-substring-with-at-least-k-repeating-characters
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ public class S ...
- [leetcode]Reorder List @ Python
原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...