Home > Blockchain >  Is there any other way to use different function name for __init__ in python?
Is there any other way to use different function name for __init__ in python?

Time:05-15

for example: for code like this,

class Solution:
    def __init__(self, N, weights):

can i write like this

class Solution:
    def snowball(self, N, weights):

if not then why?

CodePudding user response:

No this is a magic method of class, so you can't overwrite it. The snowball method you call is not a magic, and you can't "declare" it as a magic method. The snowball method is just a normal method of class's. You can't initialize a new instance with it.

For more (https://www.tutorialsteacher.com/python/magic-methods-in-python)

CodePudding user response:

I don't understand why would you like to change the constructor method (init). The init method lets the class initialize the object's attributes and serves no other purpose. Aditionally, as all the functions that starts with double underscore, the constructor method it's a special method and it's only purpose it's to construct the class. If you are willing to change that name, you could rename it like:

__init__ = __newconstructorname__

You can check more info here: https://www.udacity.com/blog/2021/11/__init__-in-python-an-overview.html

  • Related