错误类型错误:“ str”对象不可调用

时间:2018-12-01 05:44:04

标签: python python-3.x

运行笔记本new_project时出现错误:

  

self.notes.append(参与者(工作,pname,pstage,参与者))
      TypeError:“ str”对象不可调用

有人知道为什么吗?我的new_project代码正确吗?还是会有另一个错误?请帮忙,我仍在研究Python,现在尝试在代码中应用继承

这是代码

import datetime


last_id = 0


class project:

    def __init__(self, job, pname, pstages):
        self.pname = pname
        self.pstages = pstages
        self.job = job
        global last_id
        last_id += 1
        self.id = last_id

        self.list_of_project=[]


    def match(self, filter):

        return filter in self.pname

class participant(project):

    def __init__(self, job, pname, pstages, participant, pid):
        super(participant,self).__init__(job, pname, pstages)
        self.participant = participant  
        self.pid = project.id
        self.list_of_participants=[]

class notebook:

    def __init__(self):
        self.notes = []

    def new_project(self, job, pname, pstages, participant):
        self.notes.append(participant(job, pname, pstages, participant))

    def new_pstages(self, pstages, pname=''):
        self.notes.append(project(pstages, pname))

    def _find_project(self, project_id):
        for project in self.notes:
            if str(project.id) == str(project_id):
                return project

        return None

    def modify_pstages(self, project_id, pstages):
        project = self._find_project(project_id)
        if project:
            project.pstages = pstages
            return True
        return False

    def search(self, filter):
        return [project for project in self.notes if
                project.match(filter)]

2 个答案:

答案 0 :(得分:0)

这里:

def new_project(self, job, pname, pstages, participant):
    self.notes.append(participant(job, pname, pstages, participant))

您的函数有一个名为participant的参数,因此它试图将其作为函数调用。您可以像这样重命名函数参数:

def new_project(self, job, pname, pstages, part): # <- choose any name you want
    self.notes.append(participant(job, pname, pstages, part))

,或者您可以重命名participant类。按照惯例,类名通常大写:

def new_project(self, job, pname, pstages, participant):
    self.notes.append(Participant(job, pname, pstages, participant))

请注意,如果重命名该类,则必须在代码中的其他任何地方更改其名称。

答案 1 :(得分:0)

new_project参数中的参与者将覆盖名为参与者的类:

class participant(project):

[..]


    def new_project(self, job, pname, pstages,  --> participant <--):
        self.notes.append( --> participant <-- (job, pname, pstages, participant))

您最终尝试使用参数job, pname, pstages, participant

调用字符串