如何在函數的定義中調用self.value
?如何在python的類函數定義中調用self.value?
class toto :
def __init__(self):
self.titi = "titi"
def printiti(self,titi=self.titi):
print(titi)
如何在函數的定義中調用self.value
?如何在python的類函數定義中調用self.value?
class toto :
def __init__(self):
self.titi = "titi"
def printiti(self,titi=self.titi):
print(titi)
這是它是如何做:
def printiti(self, titi=None):
if titi is None:
titi = self.titi
print titi
這是一種常見的蟒蛇成語(參數設定爲無默認值和方法的身體檢查它)。
感謝您的幫助Regards Bussiere – user462794 2010-09-30 12:34:30
它不能在py3k上工作:-) – joaquin 2010-09-30 12:38:51
猜猜看是什麼,print是3k中的函數:)反而寫:'print(titi)' – gruszczy 2010-09-30 12:48:18
class Toto:
def __init__(self):
self.titi = "titi"
def printiti(self, titi=None):
if titi is None:
titi = self.titi
print(titi)
類名通常是大寫字母。
感謝您的幫助Bussiere – user462794 2010-09-30 12:34:46
+1以下pep8 – Daenyth 2010-09-30 13:15:21
@Daenyth:我討厭PEP8的部分內容。由於問題的複製和粘貼,合規性完全是偶然的。不要感謝我。謝謝user462794。 – 2010-09-30 15:15:26
你正在使用什麼Python教程? – 2010-09-30 12:28:19
@ S.Lott:它與py3k有什麼關係? – SilentGhost 2010-09-30 12:33:38
此問題已在此處得到解答:http://stackoverflow.com/questions/1802971/nameerror-name-self-is-not-defined/1802980#1802980嘗試首先搜索 – Franck 2010-09-30 12:34:18