我在python中调用了一个带有许多可选参数的类。我以这种方式构造构造函数的原因是因为我想有两种可能性来创建一个雇员对象的实例。
def __init__(self, ssn=None, familyssn=None, first_name=None, last_name=None, middle_name=None, email=None, mobile_phone=None, phone=None, department=None, salary=None, employee_list=None):
self.employee_list = employee_list or []
dict_of_emp = {}
list_of_emp = []
self.ssn = ssn
self.familyssn = familyssn
self.first_name = first_name
self.last_name = last_name
self.middle_name = middle_name
self.email = email
self.mobile_phone = mobile_phone
self.phone = phone
self.department = department
self.salary = salary
list_of_emp.append(ssn)
list_of_emp.append(familyssn)
list_of_emp.append(first_name)
list_of_emp.append(last_name)
list_of_emp.append(middle_name)
list_of_emp.append(email)
list_of_emp.append(mobile_phone)
list_of_emp.append(phone)
list_of_emp.append(department)
list_of_emp.append(salary)
#print "listi i smid: ", list_of_emp
Employee.empCount += 1
print "loop through list in constructor: ", employee_list
这是调用构造函数的唯一方法吗?
emp = Employee(None, None, None, None, None, None, None, None, None, None, list_of_list_emp_to_register)
我发现它的语法相当丑陋!
答案 0 :(得分:0)
没有。选项,如你所说的可选。因此Employee()
相当于填写所有None
s。这就是ssn=None
等等。
要指定最后一个参数,只需说
emp = Employee(employee_list=list_of_list_emp_to_register)