|
![](/i/fill.gif) |
Adding two objects of class vector:
[begin code]
class vector(object):
def __init__(self,x,y,z):
self.__x=x
self.__y=y
self.__z=z
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
@property
def z(self):
return self.__z
def __add__(self,b):
"""this overloads the + operator"""
return vector(self.x + b.x, self.y + b.y, self.z + b.z)
def __sub__(self,b):
"""this overloads the - operator"""
return vector(self.x - b.x, self.y - b.y, self.z - b.z)
a = Vector(1, 2, 3)
b = Vector(4, 5, 6)
c = a + b
[end code]
in python there's no distinction between method and function.
And for python just like any other OO language:
Every function call can return anything. Any class function/method can but need
not modify the instance. It's all up to you.
Regards
Aydan
Post a reply to this message
|
![](/i/fill.gif) |