🌐
GeeksforGeeks
geeksforgeeks.org › python › python-__add__-magic-method
Python __add__() magic method - GeeksforGeeks
July 23, 2025 - Python __add__() function is one of the magic methods in Python that returns a new object(third) i.e. the addition of the other two objects.
🌐
W3Schools
w3schools.com › PYTHON › ref_set_add.asp
Python Set add() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The add() method adds an element to the set.
Discussions

Designing class with __add__
Hello Python Experts, I have a collection of objects for which the sum operation makes sense. Therefore I do: def __add__(self, obj): #something here that adds obj to self and creates val, an instance of the #current class. return val however I have a piece of code that looks like: def get_total(): ... More on discuss.python.org
🌐 discuss.python.org
7
0
May 2, 2024
operator overloading - Difference between __add__() and add() inside the class definition in Python - Stack Overflow
I am learning OOP in Python and I came across the situation where I want to add two objects. I have seen people using the format below: class GFG: def __init__(self, val): self.val =... More on stackoverflow.com
🌐 stackoverflow.com
class - Using __add__ operator with multiple arguments in Python - Stack Overflow
I am trying to add a class object with a number, but I'm confused on how to go about adding a class object with two numbers. For example, this is my hypothetical add class method: class A: def More on stackoverflow.com
🌐 stackoverflow.com
How to overload __add__ method in a self made class to sum multiple objects of the class?
How to overload __add__ method in a self made class to sum multiple objects of the class · I have received this solution from chat gpt but this does not work. I can’t sum more than 2 objects of my class. How can i solve this?;D (sorry for bad English) · Your __add__ currently returns an ... More on discuss.python.org
🌐 discuss.python.org
5
0
December 10, 2023
🌐
ZetCode
zetcode.com › python › dunder-add
Python __add__ Method - Complete Guide
April 8, 2025 - This comprehensive guide explores Python's __add__ method, the special method that implements the addition operator (+).
🌐
Codingem
codingem.com › home › python __add__() method
Python __add__() Method - codingem.com
April 10, 2023 - The __add__() method in Python is a special method that defines what happens when you add two objects with the + operator.
🌐
Finxter
blog.finxter.com › home › learn python blog › python __add__() magic method
Python __add__() Magic Method - Be on the Right Side of Change
June 9, 2022 - Syntax object.__add__(self, other) Python’s object.__add__(self, other) method returns a new object that represents the sum of two objects. It implements the addition operator + in Python. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). ...
🌐
Medium
aignishant.medium.com › understanding-pythons-add-and-iadd-magic-methods-b8eb8821dea1
Understanding Python’s __add__ and __iadd__ Magic Methods | by Nishant Gupta | Medium
March 27, 2023 - These methods allow developers to define addition and in-place addition behavior for their custom classes, enabling instances of those classes to be used with the standard arithmetic operators + and +=. Understanding how these methods work and when to use them can greatly improve the usability and readability of your code, making it more Pythonic and user-friendly.
🌐
LabEx
labex.io › questions › what-is-the-add-method-649962
What is the __add__ method? | LabEx
November 12, 2025 - The __add__ method in Python is a special method (also known as a magic method) that allows you to define the behavior of the addition operator (+) for instances of a class.
🌐
Python.org
discuss.python.org › python help
Designing class with __add__ - Python Help - Discussions on Python.org
May 2, 2024 - Hello Python Experts, I have a collection of objects for which the sum operation makes sense. Therefore I do: def __add__(self, obj): #something here that adds obj to self and creates val, an instance of the #current class. return val however I have a piece of code that looks like: def get_total(): l_obj = [] for iobj in range(10): #get object here l_obj.append(obj) tot = sum(l_obj) return tot Here sum adds to zero, the number.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › set-add-python
Set add() Method in Python - GeeksforGeeks
April 18, 2026 - Mutable types such as lists or dictionaries cannot be added.
🌐
Stack Overflow
stackoverflow.com › questions › 78368049 › difference-between-add-and-add-inside-the-class-definition-in-python
operator overloading - Difference between __add__() and add() inside the class definition in Python - Stack Overflow
1. __ add__( ) : This is a special method in Python, also known as a "magic method", its primary purpose is to define how the addition operator (+) behaves when used with objects of your class.
🌐
Codecademy
codecademy.com › docs › python › sets › .add()
Python | Sets | .add() | Codecademy
July 26, 2025 - Adds a single element to a set if it is not already present.
🌐
LinkedIn
linkedin.com › pulse › pythons-magic-method-add-swarooprani-manoor-hjmcc
Python's Magic Method: __add__
January 26, 2025 - The error happens because Python isn't naturally skilled at adding custom objects, like our "Distance" objects. It's a bit like teaching Python a new language. Because it only knows to work on primitive types such as numbers and strings, and isn't aware of how to deal with user defined data types (Objects of classes).
🌐
Python.org
discuss.python.org › python help
How to overload __add__ method in a self made class to sum multiple objects of the class? - Python Help - Discussions on Python.org
December 10, 2023 - class MyClass: def __init__(self, value): self.value = value def __add__(self, other): if isinstance(other, MyClass): return self.value + other.value elif isinstance(other, in…
🌐
Codecademy
codecademy.com › forum_questions › 5479afac7c82cafda30024c2
How do I add two numbers using __add__ ? | Codecademy
oh yeah and, overriding, not overloading, that’s something else, and doesn’t exist in python - it is when there are multiple versions of a function or method all having the same name but different parameters: https://en.wikipedia.org/wiki/Function_overloading ... Here’s an example that uses a class Ruler that works with feet and inches. Adding two Ruler objects results in a new Ruler object, with numbers of inches that exceed 12 carried over to feet.
🌐
Reddit
reddit.com › r/learnpython › how does this code work? (classes, __add__ method)
r/learnpython on Reddit: How does this code work? (classes, __add__ method)
December 3, 2018 -

I can't wrap my mind around how this code works.

class Color:
    def __init__(self, red, blue, green):
        self.red = red
        self.blue = blue
        self.green = green

    def __repr__(self):
        return "Color with RGB = ({red}, {blue}, {green})".format(red=self.red, blue=self.blue, green=self.green)

    def __add__(self, other):
        new_red = min(self.red + other.red, 255)
        new_blue = min(self.blue + other.blue, 255)
        new_green = min(self.green + other.green, 255)
        return Color(new_red, new_blue, new_green)


red = Color(255, 0, 0)
blue = Color(0, 255, 0)
green = Color(0, 0, 255)

magenta = red + blue
cyan = blue + green
yellow = red + green
white = red + blue + green

print(magenta, cyan, yellow, white, sep="\n")

The main confusion stems from the __add__ method and everything below it. What exactly are the variables below it assigned?

I assume that the argument for other is provided by adding a second object to the first object such as magenta = red + blue but I don't really get how the method finds other.blue blue between 3 different variables. Or does it create all 3 variables when __add__ is used, return them all but only other.blue results in 255? But the other two do not return 255, so it's not really a default value and seems to only be assigned if self.red and other.blue are both more than 0?

According to print(help(min)) :

With a single iterable argument, return its smallest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the smallest argument.

But in this case, there are two arguments - neither of which have a comparable value (unless you count alphabetical order), and a default number of 255 which isn't even an object? So what exactly does the min() function do in this code?

And how does white = red + blue + green work? There are 3 arguments and __add__ only accepts two. Or does it add the first two together and save it in some kind of temporary memory while adding green to it?

I've never been more lost.

Top answer
1 of 5
3
When you do red + blue, that's just a different way of writing red.__add__(blue). So inside the method, red is renamed to self, and blue is renamed to other. Both red and blue (aka self and other) are instances of Color. Therefore both have all 3 colors as attributes. I think you are confused because you have 2 completely separate variables both called "red" (and "blue" and "green"). Your code would work just as well if we renamed those globals: class Color: def __init__(self, red, blue, green): self.red = red self.blue = blue self.green = green def __repr__(self): return "Color with RGB = ({red}, {blue}, {green})".format(red=self.red, blue=self.blue, green=self.green) def __add__(self, other): new_red = min(self.red + other.red, 255) new_blue = min(self.blue + other.blue, 255) new_green = min(self.green + other.green, 255) return Color(new_red, new_blue, new_green) tom = Color(255, 0, 0) dick = Color(0, 255, 0) harry = Color(0, 0, 255) magenta = tom + dick cyan = dick + harry yellow = tom + harry white = tom + dick + harry print(magenta, cyan, yellow, white, sep="\n") All the min does is make sure that the result does not go above 255, since thats the max for RGB values. So if the add operation is greater than 255, use 255 instead.
2 of 5
2
The __add__ method allows you to do operator overloading for the + operator. When you do thing1 + thing2 in Python, that is directly equivalent to doing: thing1.__add__(thing2) You can imagine the following function: def add_color(color1, color2): new_red = min(color1.red + color2.red, 255) new_blue = min(color1.blue + color2.blue, 255) new_green = min(color1.green + color2.green, 255) return Color(new_red, new_blue, new_green) This is exactly what the method does; the only difference is color1 is self (since it is an object of class Color), and color2 is called other. This is just convention in Python; you could call other something else like color2 if you like. Either way, the method __add__ overloads + so that it calls this function when you do +. I think your main confusion comes from the following: red = Color(255, 0, 0) blue = Color(0, 255, 0) green = Color(0, 0, 255) Note that these three variables are just named red, green, blue, but that name has nothing to do with the attributes red, green, blue of the variables themselves. So the red variable has attributes red, green, and blue telling how much of each is inside it. The color red doesn't have any green or blue in it, so red.red is 255, red.blue is 0, and red.green is zero. These are the attributes assigned in the initializer method. Every Color object will have those properties. Maybe it would be less confusing if you called them color1, color2, color3 instead. Because now you're looking at the amount of red of color1, instead of the amount of red of red (which is a little weird). Calling them red, green, blue is definitely not a bad thing since those variables do represent those colors respectively. But, just for yourself as an exercise, it might make more sense to call them something else at first to separate the object red from its attribute red. With the add method implemented, you can make a new color by "adding" the reds together, "adding" the blues together, and "adding" the greens together. This doesn't mix them like paint or something, it adds them in the sense of additive colors . In this model, with 8-bit colors, (255, 0, 0) is red, (255, 255, 0) is yellow, (255, 255, 255) is white, and so on. So white is equivalent to red + green + blue because it's more or less doing (255, 0, 0) + (0, 255, 0) + (0, 0, 255) which is equal to (255, 255, 255) if you add them element-wise. The purpose of min() here is to create a saturated integer. Saturated values are those which are capped at some upper and/or lower end. This color variable is meant to only store 8-bit colors, i.e., values between 0 and 255. The point is that a color that is fully red, like (255, 0, 0) can't get any more red. So adding two reds shouldn't produce (510, 0, 0)---it should just produce (255, 0, 0). So you sum the red values, and if it's over 255, you cap it (saturate it) at 255. Same with the other colors. Small point, you should consider using the __str__ method instead of the __repr__ for the nice print. __repr__ is usually used for an unambiguous representation of the color; for e.g., it usually is the exact string you would type to create that color (e.g. red = Color(255, 0, 0), so the repr should return Color(255, 0, 0)). __str__ on the other hand is used to create user-readable versions of the color, so would be more appropriate for the message version like you have in code.
🌐
Codefinity
codefinity.com › courses › v2 › 102a5c09-d0fd-4d74-b116-a7f25cb8d9fe › dda11c29-2447-49e9-b362-e188fe0f3a5d › d305e4db-0e9c-4fd9-9b68-3b6d70ce2870
Learn Adding Single Elements to a Set | Mastering Python Sets
Python Data Structures · Start Learning · Swipe to show menu · You can also add elements to a set. To do this, there's a specific add() method. It's important to note that with this method, you can add only one element at a time. So, if you want to add 4 elements, for example, you'll need to use this method 4 times.