【问题标题】:Python3 Class Error: TypeError Employees() takes no argumentsPython3 类错误:TypeError Employees() 没有参数
【发布时间】:2023-01-25 22:00:23
【问题描述】:

我找不到这个用例的答案,因为我的语法似乎是正确的。如果我的理解(我仍在学习 python 基础知识)是正确的,我想我的代码可能没有正确地提取导入:

from Employees import Employees

e1 = Employees("Bob", "Sales", "Director of Sales", 1000000, 20)
e2 = Employees("Linda", "Executive", "CIO", 150000, 35)

print(e1.name)
print(e2.role)

以下是员工: 类员工:

def _init_(self, name, department, role, salary, years_employed): 
    self.name = name
    self.department = department
    self.role = role
    self.salary = salary
    self.years_employed = years_employed

【问题讨论】:

  • 你的在里面陈述不正确。请注意,init 前面有 2 个下划线,后面有 2 个下划线。你只在两边加了一个下划线。即 __init__(self, ...) NOT _init_(self, ...)

标签: python-3.x python-class


【解决方案1】:

只需像这样更改行 - def __init__(self, name, department, role, salary, years_employed):

那它应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多