Bubble Bubble Bubble Gut
Bubble Bubble Bubble Gut

Reputation: 3358

What's the purpose of overwriting __new__ here?

I was reading the code for the Python intervaltree module and saw the following code, (source code here):

class Interval(namedtuple('IntervalBase', ['begin', 'end', 'data'])):
    __slots__ = ()  # Saves memory, avoiding the need to create __dict__ for each interval

    def __new__(cls, begin, end, data=None):
        return super(Interval, cls).__new__(cls, begin, end, data)

It doesn't seem to do anything extra in the __new__ function, so what is the purpose of overwriting it here?

Any help will be really appreciated! Thank you!

Upvotes: 4

Views: 72

Answers (1)

wim
wim

Reputation: 362627

The parent class is:

namedtuple('IntervalBase', ['begin', 'end', 'data'])

The parameters begin, end, and data are all required parameters here.

In Interval, the argspec has changed: the data is now optional and defaults to None.

Upvotes: 5

Related Questions