Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Math

Can someone explain to me the main difference between using def__mul__ and def__rmul__ when adding to class

Can someone explain to me the main difference between using def_mul_ and def_rmul_ when adding to class?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,665 Points

Good question! The __mul__ method is needed if your class will be used in a numeric context on the left side of the multiplication operator. The __rmul__ method is needed if your class will be used in a numeric context on the reflected side, that is the right side of the multiplication operator.

Post back if you need more help. Good luck!!

Ernestas Petruoka
Ernestas Petruoka
1,856 Points

Can you give some examples? I completely lost in object oriented python course and that super anoying so please help me :(

Chris Freeman
Chris Freeman
Treehouse Moderator 67,665 Points

Hi Ernestas,

The methods __mul__ and __rmul__ work in the same fashion as __add__ and __radd__. Without __mul__, an object could not be used on the left-side of a multiplication operator. Without __rmul__, an object could not be used on the right-side (or reflected side) of a multiplication operator.

Say there is a class called Dice that can be used in a numeric context, that is, with operators addition (+) and multiplication (*). I've created a version of the D6 and Die classes where each method will print as it runs. You may follow the flow of methods for addition and multiplication below.

For coding simplification, in addition, the __radd__ method calls the __add__ method and, in multiplication, the __radd__ method calls the __add__ method.

$ python
Python 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
# import D6 class (a six-sided Die)
>>> from dice import D6
# instantiate two D6 instances with values 3 and 4
>>> d1 = D6(value=3)
running D6(3).__init__
running Die(3).__init__
>>> d2 = D6(value=4)
running D6(4).__init__
running Die(4).__init__
# add them together
>>> d1 + d2
running D6(3).__add__ with other <dice.D6 object at 0x7f84856f8668>
running Die(3).__int__
running D6(4).__radd__ with self + other 3
running D6(4).__add__ with other 3
running Die(4).__int__
7
# add them in reverse order
>>> d2 + d1
running D6(4).__add__ with other <dice.D6 object at 0x7f84856f85f8>
running Die(4).__int__
running D6(3).__radd__ with self + other 4
running D6(3).__add__ with other 4
running Die(3).__int__
7
# multiply them together
>>> d1 * d2
running D6(3).__mul__ with other <dice.D6 object at 0x7f84856f8668>
running Die(3).__int__
running D6(4).__mul__ with self * other 3
running D6(4).__mul__ with other 3
running Die(4).__int__
12
# multiply them in reverse order
>>> d2 * d1
running D6(4).__mul__ with other <dice.D6 object at 0x7f84856f85f8>
running Die(4).__int__
running D6(3).__mul__ with self * other 4
running D6(3).__mul__ with other 4
running Die(3).__int__
12

Post back if you have more questions. Good luck!!