From the course: Advanced Python: Object-Oriented Programming
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Numbers - Python Tutorial
From the course: Advanced Python: Object-Oriented Programming
Numbers
- [Instructor] Sometimes you like to emulate numbers in your values. Sometimes like in the case of the decimal, in the standard library, it's actually a number. It's a fixed fronting point, which is better accuracy floating point. If you want to emulate a number, you can implement all of these methods. So the add is for the plus sign, the sub is for the minus sign, mul is for the multiplication sign, et cetera, et cetera. There are shift left and right left, and xor and other things. And we also have radd and rsub, which are basically for the plus-equal, et cetera. Let's see an example. So here we have a time duration, so the class Duration, and we have units which are nanoseconds, microseconds, and milliseconds. And this is the multiplication from nanoseconds for each of them. In the unit, we get the value, which is a float, and the unit, which is a str. We do some data validation and then the sign. We also add a nice wrapper so we can see the results. Always add a wrapper to your…