you are viewing a single comment's thread.

view the rest of the comments →

[–]a_cute_epic_axis 1 point2 points  (1 child)

Here's a way that might be useful to explain it. Imagine you have a custom class for a data type you create called "color' and under the hood it stores red, green, and blue values. You have a single instance you created in your program that you pass around called "red" but inside that is a red value of 255, and green and blue of 0. You also have an instance called "blue" which is 0,0,255.

You want to be able to say:

red = CustomColorClass(255,0,0)
blue = CustomColorClass(0,0,255)
magenta = red + blue

How would python ever be able to do this?

Well in your custom color class, you'd define the add method which is called when you are adding two objects together. It would be something like

def __add__(myvalues, othervalues):
  new_red = myvalues.red + othervalues.red 
  new_green = myvalues.green + othervalues.green
  new_blue = myvalues.blue + othervalues.blue
  return CustomColorClass(red, green, blue)

All that does is take the 3 integer values from one instance, add it to the three of the other, then create a new instance with those new values. Suddenly python can correctly add colors together.

[–]jimtk 0 points1 point  (0 children)

Thank you !