-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-121798: Add class method Decimal.from_number() #121801
base: main
Are you sure you want to change the base?
Changes from 1 commit
c76ebf2
9bb70f2
e38d8f9
ae4c7a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -582,6 +582,21 @@ def __new__(cls, value="0", context=None): | |
|
||
raise TypeError("Cannot convert %r to Decimal" % value) | ||
|
||
@classmethod | ||
def from_number(cls, number): | ||
"""Converts a real number to a decimal number, exactly. | ||
|
||
>>> Decimal.from_number(314) # int | ||
Decimal('314') | ||
>>> Decimal.from_number(0.1) # float | ||
Decimal('0.1000000000000000055511151231257827021181583404541015625') | ||
>>> 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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). |
||
return cls(number) | ||
raise TypeError("Cannot convert %r to Decimal" % number) | ||
|
||
@classmethod | ||
def from_float(cls, f): | ||
"""Converts a float to a decimal number, exactly. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add alternative :class:`~decimal.Decimal` constructor | ||
:meth:`Decimal.from_number() <decimal.Decimal.from_number>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.