Skip to content

Instantly share code, notes, and snippets.

@thorsummoner
Created October 19, 2015 21:53
Show Gist options
  • Save thorsummoner/d09d6645b4f7fecc9c18 to your computer and use it in GitHub Desktop.
Save thorsummoner/d09d6645b4f7fecc9c18 to your computer and use it in GitHub Desktop.
Example using the '%' (mod) operator to return percent.
import numbers
class Percent(float):
def __mod__(self, other):
"""Override the builtin % to give X * Y / 100.0 """
return self / other * 100.0
if __name__ == '__main__':
from pprint import pprint
pprint(
Percent(5) % 60
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment