調用函數,當我跑下面的代碼,通過調用函數的構造獲取NameError在構造
首先 - 我再次運行此,我得到這個
>>> class PrintName:
... def __init__(self, value):
... self._value = value
... printName(self._value)
... def printName(self, value):
... for c in value:
... print c
...
>>> o = PrintName('Chaitanya')
C
h
a
i
t
a
n
y
a
>>> class PrintName:
... def __init__(self, value):
... self._value = value
... printName(self._value)
... def printName(self, value):
... for c in value:
... print c
...
>>> o = PrintName('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
NameError: global name 'printName' is not defined
我不能在構造函數中調用函數嗎?並且在執行類似的代碼時會出現偏差?
注意:我忘了使用self(例如self.printName())調用該類的局部函數。爲帖子道歉。