是否可以在python中将构造方法(__init__)的名称更改为其他名称?

问题描述 投票:-3回答:1

我们可以在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
python oop constructor init
1个回答
0
投票

最简单的方法可能是重新分配它,但是正如@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__
© www.soinside.com 2019 - 2024. All rights reserved.