Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Testing Be Assertive Quantitative Assertions

Austin Hawkins-Seagram
Austin Hawkins-Seagram
5,720 Points

why does paper > rock?

in moves.py I see that Rock and Paper both have better_than and less_than attributes, but are those special names that assertGreater and assertLess can understand? I can't find any place where moves.Rock() < moves.Paper() is specified other than as an attribute.

1 Answer

Benjamin Lange
Benjamin Lange
16,178 Points

Great question! Just setting the better_than and worse_than attributes in the moves.py is not enough to allow comparing moves.Paper and moves.Rock.

The function definitions above that in moves.py is where the real magic happens. The concept going on here is called operator overloading. Basically, we are replacing the standard ">", "<", "==", and "!=" operators for our moves class. If the comparison operator is called on our classes, such as moves.Paper() > moves.Rock(), we will use our special operator overload methods.

The operators are overloaded using the special function names gt (greater than), lt (less than), eq (equal), and ne (not equal). So, when you do moves.Paper() > moves.Rock(), it calls our custom gt method. This method then checks to see if the moves.Rock() (other) class name is in Paper() better_than list. If it is, it returns true. Otherwise, it returns false.

Check out this page for even more information and a few interactive code samples. Python Operator Overloading

Let me know if that makes a little more sense now.

Austin Hawkins-Seagram
Austin Hawkins-Seagram
5,720 Points

That makes a lot more sense. thank you