Class member function: __init__ , __sub__ : Class sub « Class « Python






Class member function: __init__ , __sub__

Class member function: __init__ , __sub__

class Number:
    def __init__(self, start):              # on Number(start)
        self.data = start
    def __sub__(self, other):               # on instance - other
        return Number(self.data - other)    # result is a new instance


X = Number(5)                           # Number.__init__(X, 5)
Y = X - 2                               # Number.__sub__(X, 2)
print Y.data                                  # Y is new Number instance

           
       








Related examples in the same category