wiki:python/Special methods/__new__

__add__, __class__, __cmp__, __dict__, __eq__, __format__, __getstate__, __gt__, __hash__, __iadd__, __len__, __lt__, __new__, __repr__, __setstate__, __subclasses__


Python Doc


Unterschied zwischen __init__ und __new__
Verständnisfrage zu `__new__()`
Overriding __new__ for attribute initialization (Python recipe)
Parameterübergabe Kind- an Elternklasse...


Examples

Inherit from datetime.datetime

`__getattribute__` Rekursion umgehen?

class DateTime(datetime.datetime):
    def __new__(cls, dt_values):
        return datetime.datetime.__new__(cls, *dt_values)
        
    def replace(self, **kwargs):
        return DateTime(
            datetime.datetime.replace(self, **kwargs).timetuple()[0:5]
        )
>>> dt.DateTime((2014, 4, 17))
DateTime(2014, 4, 17, 0, 0)
>>> _.replace(year=2015)
DateTime(2015, 4, 17, 0, 0)
Last modified 5 years ago Last modified on Aug 31, 2014, 2:37:21 PM