2
1
Jacob Zimmerman's profile photoThe Digital Cat's profile photo
 
The example is fine and all, but I think that the Binary class should probably have the reversed operators, like _radd_ and _rsub_, that returns the type taken in. So, if you add 3 + Binary(3), you'll get 6 (not Binary(6)).
After all, you're creating a number class, and getting all of those operators into place are highly recommended.
I wouldn't be surprised if you're saving it for a later part, though.

One more thing: your _str_ implementation could be shortened to bin(self)[2:] instead of str(bin(self))[2:] since bin() returns a string anyway.
 
You are right, honestly I did not think about _radd_() and _rsub_(). I'll try and find a way to add them to the post or to the second part. About the _str_() implementation your change makes perfectly sense.

Thank you!
Add a comment...