首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python3- __init__()从1到5个位置参数,但给出了6个

Python3- __init__()从1到5个位置参数,但给出了6个
EN

Stack Overflow用户
提问于 2020-10-14 04:55:35
回答 2查看 1.3K关注 0票数 0

我第一次尝试在python中实现linked_lists,但我的附加函数似乎不能正常工作。以下是目前为止的代码:

代码语言:javascript
运行
复制
class node:
    def __init__(self, Name = None, Contact_number = None, Type = None, Urgency = None):
        self.Name = Name
        self.Contact_number = Contact_number
        self.Type = Type
        self.Urgency = Urgency
        self.next = None

class linked_list:
    def __init__(self):
        self.head = node()
    
    def append(self, Name, Contact_number, Type, Urgency):
        new_node = node(self,Name,Contact_number,Type,Urgency)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node
    
    def length(self):
        cur = self.head()
        total = 0
        while cur.next != None:
            total+=1
            cur=cur.next
        return total
    
    def display(self):
        Appointments = []
        cur_node = self.head
        while cur_node.next != None:
            cur_node = cur_node.next
            aptmnt = (cur_node.Name, cur_node.Contact_number, cur_node.Type, cur_node.Urgency)
            Appointments.append(aptmnt)
        print(Appointments)
    
    
general_surgeon = linked_list()
general_surgeon.display()

general_surgeon.append("Akash", "827xxxxxx1", "Min Surgery", "no")

下面是我尝试使用附加函数时遇到的错误:

代码语言:javascript
运行
复制
TypeError                                 Traceback (most recent call last)
<ipython-input-27-0ffb4f93a9c0> in <module>
     39 general_surgeon.display()
     40 
---> 41 general_surgeon.append("Akash", "827xxxxxx1", "Min Surgery", "no")

<ipython-input-27-0ffb4f93a9c0> in append(self, Name, Contact_number, Type, Urgency)
     12 
     13     def append(self, Name, Contact_number, Type, Urgency):
---> 14         new_node = node(self,Name,Contact_number,Type,Urgency)
     15         cur = self.head
     16         while cur.next != None:

TypeError: __init__() takes from 1 to 5 positional arguments but 6 were given

我一直在直接从youtube视频中复制链接列表的基本结构和功能,除了我的代码有更多的数据变量之外,我注意到我的代码和他的没有区别。

请帮帮忙?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-14 04:57:40

代码语言:javascript
运行
复制
def append(self, Name, Contact_number, Type, Urgency):
    new_node = node(self,Name,Contact_number,Type,Urgency)

因为要调用node类,所以不需要传递self参数。

票数 1
EN

Stack Overflow用户

发布于 2020-10-14 05:01:29

下面是错误,它已经显示为一个TypeError

代码语言:javascript
运行
复制
TypeError: __init__() takes from 1 to 5 positional arguments but 6 were given

所以你在line # 14中给出了6个参数,而不是5个

无需将self作为参数传递到node class at line # 14

试着用这个:

代码语言:javascript
运行
复制
def append(self, Name, Contact_number, Type, Urgency):
    new_node = node(Name, Contact_number, Type, Urgency)
    cur = self.head
    while cur.next != None:
        cur = cur.next
    cur.next = new_node
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64346826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档