I read here and here that Python methods can be aliased using =.
However, when I try it on the __init__ method, it doesn't seem to work.
class MyClass:
def __init__(self):
print("Hi mum!")
new_name = __init__
a = MyClass()
b = MyClass.new_name()
The b = MyClass.new_name() causes this error:
TypeError: __init__() missing 1 required positional argument: 'self'
Why does this not work? And how should I alias __init__ correctly?
new_namestatic or class method that returns newMyClassobject.__init__method expects the positional argumentself, as you can see by it's definition. When you call thenew_namemethod explicitly from the class, there is no positional argument passed, like there is implicitly when instantiating a new objectnew_namea static method seems inconvenient because if the parameters of__init__change, then I also have to update the parameters ofnew_name, and the arguments toMyClass(...)insidenew_name.__new__method of a class to create an instance without it automatically calling the__init__method. So you could do something likeb = MyClass.new_name(MyClass.__new__(MyClass)). You'll have to modify the__init__method to returnselfthough, so thatbpoints to the instance created