Class member function: __init__ , __sub__ : Class sub : Class : Python examples (example source code) Organized by topic

C++
PHP
Python
Python Home »  Class   » [  Class sub  ]  Screenshots 
 



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
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.