Python - Subtraction method:__sub__

Introduction

The following class Number implement two methods: constructor and subtraction.

Demo

class Number: 
    def __init__(self, start):                  # On Number(start) 
        self.data = start #  w  w w.ja v a  2s .  co  m
    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