Class member function : Class sub : Class PYTHON examples


PYTHON examples » Class » Class sub »

 

Class member function


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 Scripts with Example Source Code in same category :

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON examples

 Navioo Class
» Class sub