POV-Ray : Newsgroups : povray.off-topic : OO theory? Python. : Re: OO theory? Python. Server Time
28 Jul 2024 22:22:37 EDT (-0400)
  Re: OO theory? Python.  
From: Aydan
Date: 15 Oct 2012 11:55:00
Message: <web.507c30f57215dcd3771cd8e0@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.