【问题标题】:TypeError: 'Employee' object is not callableTypeError:“员工”对象不可调用
【发布时间】:2022-01-22 03:18:54
【问题描述】:

我有课

class Employee(BaseModel):
    Emp_ID: int
    Emp_Name: str
    Emp_Qualification: str

下面是 POST 请求的代码,我在最后一次返回时遇到错误,我试图获取employee["Emp_id"] 值。

@app.api_route("/post-details/{postid}", methods=["POST"])        
def post(postid: int , employee : Employee):
        newlist = []
        cursor=conn.cursor()
        getemploy2= "select id from employee3"
        cursor.execute(getemploy2)
        result = cursor.fetchall()
        for list in result:
                newlist.extend(list)
        if postid in newlist:
                return "yes it exist"
        return employee(["Emp_ID"])

【问题讨论】:

  • 要获取对象的属性,既不能使用(),也不能使用[],也不能使用""。你的意思是return employee.Emp_ID
  • 当访问一个实例的属性时,你必须这样做:employee.Emp_ID,而不是方括号和圆括号。我建议你先学习更多关于 Python 的 OOP :)
  • 很棒的 mkrieger,谢谢

标签: python api


【解决方案1】:

您可以使用. 运算符访问对象的属性:

return employee.Emp_ID

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2021-04-15
    • 2011-10-01
    • 2020-11-10
    • 2017-09-09
    • 2016-07-23
    相关资源
    最近更新 更多