python - 错误类型错误:“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)]
解决方案
这里:
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))
请注意,如果您重命名该类,则必须在代码中的其他任何地方更改其名称。
推荐阅读
- c++ - 了解类共享指针及其在继承中的使用
- typescript - 如何扩展用于类型检查的递归打字稿接口?
- java - javabridge.jutil.JVMNotFoundError:找不到Java虚拟机
- json - Python AWS lambda JSON序列化问题
- clickhouse - 以毫秒为单位的 clickhouse dateTime
- r - R中的历史股票价格下载
- jquery - 我如何使用 jquery 计算 asp.net mvc 上的文本框中留下的字符
- jquery - jquery 输入类型文件 *.xlsx
- php - php核心贝宝sdk
- java - Maven 资源插件不会过滤掉嵌套在 ${} 中的自定义分隔符