Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—如何生成一个类方法,该类方法接收任意数量的参数并返回所有参数的列表?_Python_Python 3.x_Class_Inheritance - Fatal编程技术网

Python—如何生成一个类方法,该类方法接收任意数量的参数并返回所有参数的列表?

Python—如何生成一个类方法,该类方法接收任意数量的参数并返回所有参数的列表?,python,python-3.x,class,inheritance,Python,Python 3.x,Class,Inheritance,我创建了一个名为Manager的类作为Employee的子类,并尝试创建一个常规方法,用于将员工添加到经理的监督中。但是,我希望此方法能够接收任意数量的参数(员工数量),因此我在定义方法时添加了*: class Employee: def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay # Returns e

我创建了一个名为Manager的类作为Employee的子类,并尝试创建一个常规方法,用于将员工添加到经理的监督中。但是,我希望此方法能够接收任意数量的参数(员工数量),因此我在定义方法时添加了*:

class Employee:

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay

    # Returns employees fullname
    def fullname(self):
        return '{} {}'.format(self.first, self.last)


class Manager(Employee):

    def __init__(self, first, last, pay, employees=None):
        super().__init__(first, last, pay)
        if employees is None:
            self.employees = []
        else:
            self.employees = employees

    def add_emp(self, *emp):           # Accept any no. of args
        if emp not in self.employees:
            self.employees.append(emp)

    def print_emps(self):              # Prints all employees' names
        for emp in self.employees:
            print('-->', emp.fullname())
当我运行addind emp(emp_1、emp_2、emp_3)这样的代码时,会添加它们,但在打印它们的名称时会出现错误:

emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Test', 'User', 60000)
mng_1 = Manager('Roger', 'Smith', 100000)

mng_1.add_emp(emp_1, emp_2)
mng_1.print_emps()

AttributeError: 'tuple' object has no attribute 'fullname'

如何输入尽可能多的参数作为列表,使类的属性和方法对每个项都有效?

更改您的add\u emp方法,如下所示:

def add_emp(self, *emps):           # Accept any no. of args
    for emp in emps:
        if emp not in self.employees:
            self.employees.append(emp)

在Employee类中,返回的值似乎是一个元组。似乎有一种方法返回多个对象,在python中这些对象被视为元组。元组对象将没有Employee属性。您需要在测试此函数的地方发布代码。@Sid如果我去掉*并将员工逐个添加为:manager.add_emp(emp_1)manager.add_emp(emp_2),它们将添加到列表中,然后我可以使用Employee属性和方法。但是如果我离开*它会自动将它们添加到一个元组中问题是
*emp
是一个员工元组(类似于列表),但是
add\u emp
中的代码将
emp
视为一个记录。您应该使用
*emp
,然后在emp:中对emp使用
,然后在其中使用您现有的代码。不将此作为答案,因为这是一个简单的逻辑错误。@JimStewart但为什么不使用for循环将参数添加到元组而不是列表中?kkkk为什么工作?这就像for循环使*add the args to a listemps成为参数的元组(类似于列表)。如果您想对它们中的每一个都做些什么,可以使用“for emp in emps”在元组上迭代