python类方法

最近学习《python参考手册》学到class部分,遇到了类的构造析构部分的问题:

1、什么时候构造?
2、什么时候析构?
3、成员变量如何处理?
4、python中的共享成员函数如何访问?
————————
探索过程:
1、经过查找,python中没有专用的构造和析构函数,但是一般可以在__init__和__del__分别完成初始化和删除操作,可用这个替代构造和析构。还有一个__new__用来定制类的创建过程,不过需要一定的配置,此处不做讨论。
2、类的成员函数默认都相当于是public的,但是默认开头为__的为私有变量,虽然是私有,但是我们还可以通过一定的手段访问到,即python不存在真正的私有变量。如:

代码如下:

__privalue = 0 # 会自动变形为”_类名__privalue”的成员变量

3、由于python的特殊性,全局成员变量是共享的,所以类的实例不会为它专门分配内容空间,类似于static,具体使用参看下面的例子。

测试1:

代码如下:

# encoding:utf8

class newclass(object):
num_count = 0 # 所有的实例都共享此变量,即不单独为每个实例分配
def __init__(self,name):
self.name = name
newclass.num_count += 1
print name,newclass.num_count
def __del__(self):
newclass.num_count -= 1
print “del”,self.name,newclass.num_count
def test():
print “aa”

aa = newclass(“hello”)
bb = newclass(“world”)
cc = newclass(“aaaa”)

print “over”

调试运行:

代码如下:

hello 1
world 2
aaaa 3
over
deexception l hello 2
attributeerror: “‘nonetype’ object has no attribute ‘num_count'” in ignored
exception attributeerror: “‘nonetype’ object has no attribute ‘num_count'” in ignored

我们发现,num_count 是全局的,当每创建一个实例,__init__()被调用,num_count 的值增一,当程序结束后,所有的实例会被析构,即调用__del__() 但是此时引发了异常。查看异常为 “nonetype” 即 析构时newclass 已经被垃圾回收,所以会产生这样的异常。

但是,疑问来了?为什么会这样?按照c/c++等语言的经验,不应该这样啊!经过查找资料,发现:

python的垃圾回收过程与常用语言的不一样,python按照字典顺序进行垃圾回收,而不是按照创建顺序进行。所以当系统进行回收资源时,会按照类名a-za-z的顺序,依次进行,我们无法掌控这里的流程。

明白这些,我们做如下尝试:

代码如下:

# encoding:utf8

class newclass(object):
num_count = 0 # 所有的实例都共享此变量,即不单独为每个实例分配
def __init__(self,name):
self.name = name
newclass.num_count += 1
print name,newclass.num_count
def __del__(self):
newclass.num_count -= 1
print “del”,self.name,newclass.num_count
def test():
print “aa”

aa = newclass(“hello”)
bb = newclass(“world”)
cc = newclass(“aaaa”)

del aa
del bb
del cc

print “over”

调试输出:

代码如下:

hello 1
world 2
aaaa 3
del hello 2
del world 1
del aaaa 0
over

ok,一切按照我们预料的顺序发生。
但是,我们总不能每次都手动回收吧?这么做python自己的垃圾回收还有什么意义?

so,继续查找,我们还可以通过self.__class__访问到类本身,然后再访问自身的共享成员变量,即 self.__class__.num_count , 将类中的newclass.num_count替换为self.__class__.num_count 编译运行,如下:

代码如下:

# encoding:utf8

class newclass(object):
num_count = 0 # 所有的实例都共享此变量,即不单独为每个实例分配
def __init__(self,name):
self.name = name
self.__class__.num_count += 1
print name,newclass.num_count
def __del__(self):
self.__class__.num_count -= 1
print “del”,self.name,self.__class__.num_count
def test():
print “aa”

aa = newclass(“hello”)
bb = newclass(“world”)
cc = newclass(“aaaa”)

print “over”

结果:

代码如下:

hello 1
world 2
aaaa 3
over
del hello 2
del world 1
del aaaa 0

perfect!我们完美地处理了这个问题!

ps:

书上又提到了一些问题,在这里作补充(仅作为参考):

__new__()是唯一在实例创建之前执行的方法,一般用在定义元类时使用。

del xxx 不会主动调用__del__方法,只有引用计数==0时,__del__()才会被执行,并且定义了__del_()的实例无法被python的循环垃圾收集器收集,所以尽量不要自定义__del__()。一般情况下,__del__() 不会破坏垃圾处理器。

实验中发现垃圾回收自动调用了__del__, 这与书上所说又不符,不知是什么原因,需要继续学习。

Posted in 未分类

发表评论