| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
It is an alternate constructor which only accepts a single numeric argument. Unlike to Decimal.from_float() it accepts also Decimal. Unlike to the standard constructor, it does not accept strings and tuples.
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
| >>> Decimal.from_number(Decimal('3.14')) # another decimal instance | ||
| Decimal('3.14') | ||
| """ | ||
| if isinstance(number, (int, Decimal, float)): |
There was a problem hiding this comment.
Sorry, something went wrong.
There was a problem hiding this comment.
Yes, these are the only numeric types accepted by the constructor. Exact conversion from Fraction to Decimal is not possible in general case (e.g. 1/3).
Sorry, something went wrong.
There was a problem hiding this comment.
But it's possible from the Integral type.
Sorry, something went wrong.
There was a problem hiding this comment.
The main constructor does not accept the Integral type.
from_number() only supports a subset of values for the main constructor.
Sorry, something went wrong.
There was a problem hiding this comment.
Ah, then this does make sense.
Sorry, something went wrong.
|
|
||
| .. classmethod:: from_number(number) | ||
|
|
||
| Alternative constructor that only accepts numbers (instances of |
There was a problem hiding this comment.
Instead of using number, maybe you can directly say "only accepts instances of float, int or Decimal" since otherwise people might wonder why objects implementing the Number protocol are not allowed.
By the way, this function is essentially a shortcut to avoid an if isinstance(...) right?
Sorry, something went wrong.
There was a problem hiding this comment.
Done.
Yes, you can look at it from such point. If your function needs a Decimal or a number that can be converted to Decimal, it can use Decimal.from_number() without additional type check. It will reject strings and tuples that are accepted by the constructor.
Sorry, something went wrong.
There was a problem hiding this comment.
+1 for the general idea. I haven't reviewed line-by-line.
Sorry, something went wrong.
| >>> Decimal.from_number(Decimal('3.14')) # another decimal instance | ||
| Decimal('3.14') | ||
| """ | ||
| if isinstance(number, (int, Decimal, float)): |
There was a problem hiding this comment.
Ah, then this does make sense.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
It is an alternate constructor which only accepts a single numeric argument. Unlike to Decimal.from_float() it accepts also Decimal. Unlike to the standard constructor, it does not accept strings and tuples.
📚 Documentation preview 📚: https://cpython-previews--121801.org.readthedocs.build/