首页 > 解决方案 > 错误类型错误:“str”对象不可调用

问题描述

当我运行 notebook new_project 时出现错误:

self.notes.append(participant(job, pname, pstages,参与者))
TypeError: 'str' object is not callable

有人知道为什么吗?我的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)]

标签: pythonpython-3.x

解决方案


这里:

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))

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


推荐阅读