Home  >  Article  >  Backend Development  >  python destructor and special call method

python destructor and special call method

高洛峰
高洛峰Original
2016-11-19 09:57:04971browse

# -*- coding: utf-8 -*-
"""
Created on Sun Nov 13 23:19:03 2016
 
@author: toby
"""
#知识点:析构函数和特殊的__call__方法
'''
析够函数:
其中的“__del__”就是一个析构函数了,当使用del 删除对象时,会调用他本身的析构函数,
另外当对象在某个作用域中调用完毕,在跳出其作用域的同时析构函数也会被调用一次,这
样可以用来释放内存空间。
'''
class Foo:
    def __init__(self):
        pass
     
    def __del__(self): #析构函数 __del__函数永远是在最后执行
        print '销毁'
         
    def go(self):
        print 'go'
               
    def __call__(self): 
        print 'call'
 
f1 = Foo() #实例化对象
f1.go() #执行go方法
f1()    #这里的调用实际上是直接执行__call__方法里面的代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn