我们可以在Python中将构造函数方法(__init__
)名称更改为其他名称吗?例子
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __newconstructorname__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@email.com'
self.pay = pay
最简单的方法可能是重新分配它,但是正如@Barmar所问:“为什么要这样做?”:
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __newconstructorname__(self, first, last, pay):
print('in init')
self.first = first
self.last = last
self.email = first + '.' + last + '@email.com'
self.pay = pay
__init__ = __newconstructorname__