pythonで_new__ の役割

411 ワード

class PositiveInteger(int):
    def __init__(self, value):
        super(PositiveInteger, self).__init__(self, abs(value))  
i = PositiveInteger(-3)
print i 

結果は-3
class PositiveInteger(int):
    def __new__(cls, value):
        return super(PositiveInteger, cls).__new__(cls, abs(value))  
i = PositiveInteger(-3)
print i 

これで3