我是 Python 的新手。有人可以幫我解決這個錯誤嗎?
我的代碼如下,
class First:
def passThisArgs(self, a, b, c):
return Second(self, a, b, c)
class Second:
def __init__(self, a, b, c):
print 'Values are :\n', a, b, c
newObj = First()
a = 5
b = 6
c = 7
newObj.passThisArgs(a, b, c)
我收到此錯誤:
Traceback (most recent call last):
File "/root/PycharmProjects/abc.py", line 13, in <module>
newObj.passThisArgs(a, b, c)
File "/root/PycharmProjects/abc.py", line 3, in passThisArgs
return Second(self, a, b, c)
TypeError: __init__() takes exactly 4 arguments (5 given)
我在 Linux 中使用 python 2.7。
uj5u.com熱心網友回復:
您不會將“自我”傳遞給另一個班級。Python 將創建一個全新的物件并自動將其作為self引數傳遞給__init__. 所以:
return Second(a,b,c)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395739.html
上一篇:更改變數屬性,相應地評估其他屬性
下一篇:如何使用類方法創建替代建構式?
