Vru*_*aut 0 python oop python-3.x
我试图以面向对象的方式在Class和Function中传递变量列表但面临一些错误,我不明白它有什么问题我在Eclipse中使用PyDev
class Mydetails:
__details = ''
def __init__(self,details): # constructor
#self.__details['country'] = details['country']
self.__details = details
def set_element(self,details):
self.__details = details
def get_list(self,details):
return self.__details
details = ['ABC','DEF','GHI','JKL']
pdetails = Mydetails()
pdetails.set_element(details)
print(pdetails.get_list())
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "C:\workspace\python\pythonapp\list.py", line 18, in <module>
pdetails = Mydetails()
TypeError: __init__() missing 1 required positional argument: 'details'
Run Code Online (Sandbox Code Playgroud)
在这一行:
def __init__(self,details):
Run Code Online (Sandbox Code Playgroud)
你使init函数取一个参数(细节),但在这里:
pdetails = Mydetails()
Run Code Online (Sandbox Code Playgroud)
你不给它一个参数!你可能想做的是:
pdetails = Mydetails(details)
Run Code Online (Sandbox Code Playgroud)
在旁注上你的def get_list(self,details):功能没有意义,你为什么要传递一个不被使用的参数?