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 Python Testing Be Assertive Quantitative Assertions

Austin Hawkins-Seagram
Austin Hawkins-Seagram
5,699 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,177 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,699 Points

That makes a lot more sense. thank you