python self 问题

self 这个大量出现在python程序里面的形参,是可以改变的,他表示可以使用本身这个类的任何对象。比如类定义了一个long的变量和一个__fun1的私有函数(对象),那么类中的函数都可以使用这个变量。

class chen:

__var1="1"

long="100"

def __fun1(self):

print "私有函数"

self.long=100

def fun2(self):

self.long="100"

self.__fun1()

当然也可以在函数里面定义,那么其他函数也能使用这个变量,比如:

class chen():

def fun1(self):

self.long = 100

def fun2(self):

print self.long



p=chen()

p.fun1()

p.fun2()

但是要先调用fun1(),那么其他函数就能使用这个long了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章