Reputation: 565
I'm aware that multiple constructors in python is not possible.
However, I found the range class has two __init__
signatures from the official docs.
class range(stop)
class range(start, stop[, step])
At first I thought it's implemented like this
def __init__(stop, start=None, step=None)
But this will change the order of parameters for start
and stop
So I check the python source code and the range class has the completely different methods here:
def __init__(self, stop): # real signature unknown; restored from __doc__
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
Can anyone explain how it works? Can we do the same in our object class?
Is it something to do with single-dispatch?
Upvotes: 0
Views: 149