简体   繁体   English

在 Python 中实例化多级 Inheritance 的问题

[英]Issue with Instantiating MultiLevel Inheritance in Python

In a particular sequence of a test case, there is a TypeError: __init__ error.在测试用例的特定序列中,有一个TypeError: __init__错误。

When I run this code with TestCode 3 before TestCode 2, there is a TypeError: __init__ .当我在 TestCode 2 之前使用 TestCode 3 运行此代码时,会出现TypeError: __init__

If I run TestCode 3 after Test Code 2, there is no error and output is displayed.如果我在测试代码 2 之后运行测试代码 3,则没有错误并显示 output。 Why?为什么?

CODE:代码:

import datetime

class Employee:

    def __init__(self, name="", salary=0, job_desc="", doj=None):
        self.name = name
        self.salary = salary
        self.description = job_desc
        self.dataOfJoing = doj
        self.no_of_years = ""

    def calRaise(self):
        today = datetime.date.today()
        self.no_of_years = today.year - self.dataOfJoing.year
        if int(self.no_of_years) > 5:
            percent = 0.20
        else:
            percent = 0
        self.salary = self.salary + (self.salary * percent)

    def display(self):
        return "Employee: name = %s, Job description= %s, No of years= %s, " \
               "Salary = %s" % (self.name, self.description, self.no_of_years, self.salary)



class Server(Employee):

    def __init__(self, name="", salary=0, job_desc="", doj=None):
        Employee.__init__(self, name, salary, job_desc, doj)


class chef(Employee):

    def __init__(self, name="", salary=0, job_desc="", doj=None):
        Employee.__init__(self, name, salary, job_desc, doj)


class PizzaRobot(chef):

    def __init__(self):
        chef.__init__(self, "Robot",0,"cooks",datetime.datetime(2012, 9, 10))

TestCode 1:测试代码 1:

server = Server("John", 400, "customer interface", datetime.date(2015, 4, 3))

server.calRaise()

print(server.display())

TestCode 3:测试代码 3:

chef = chef("Tom", 5000, "makes food", datetime.datetime(2000, 8, 13))

chef.calRaise()

print(chef.display())

TestCode 2:测试代码 2:

robot = PizzaRobot()

robot.calRaise()

print(robot.display())

ERROR Message when TestCode 3 is run above TestCode 2:当 TestCode 3 在 TestCode 2 上运行时出现错误消息:

Traceback (most recent call last): File "/Users/z10049/PycharmProjects/Q4Lab5/Q4.py", line 52, in robot = PizzaRobot() File "/Users/z10049/PycharmProjects/Q4Lab5/Q4.py", line 38, in init chef.回溯(最近一次通话最后):文件“/Users/z10049/PycharmProjects/Q4Lab5/Q4.py”,第 52 行,机器人 = PizzaRobot() 文件“/Users/z10049/PycharmProjects/Q4Lab5/Q4.py”,行38、 init厨师。 init (self, "Robot",0,"cooks",datetime.datetime(2012, 9, 10)) TypeError: init () takes from 1 to 5 positional arguments but 6 were given init (self, "Robot",0,"cooks",datetime.datetime(2012, 9, 10)) TypeError: init () 需要从 1 到 5 个位置 arguments 但给出了 6 个

It's because you reuse the class name chef for the variable in this line:这是因为您为该行中的变量重用了 class 名称chef

chef = chef("Tom", 5000, "makes food", datetime.datetime(2000, 8, 13))

Then when you create the PizzaRobot object Python tries to call the __init__ method on the object created above on this line:然后,当您创建PizzaRobot object Python 尝试在上面创建的 object 上调用__init__方法时:

chef.__init__(self, "Robot",0,"cooks",datetime.datetime(2012, 9, 10))

When Python calls a method on an object it automatically passes the object itself as the first argument to the method.当 Python 调用 object 上的方法时,它会自动将 object 本身作为该方法的第一个参数传递。 So it will essentially change the call above to:所以它本质上会将上面的调用更改为:

chef.__init__(self, self, "Robot",0,"cooks",datetime.datetime(2012, 9, 10))

That's why you're getting the error about 6 arguments instead of 5.这就是为什么您收到关于 6 arguments 而不是 5 的错误。

If you rename the class chef to Chef and keep the variable as chef you'll see that everything will work as it should.如果您将 class chef重命名为Chef并将变量保留为chef ,您将看到一切都会正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM