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了。