FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-121798: Add class method Decimal.from_number() by serhiy-storchaka · Pull Request #121801 · python/cpython · GitHub

/ cpython Public

gh-121798: Add class method Decimal.from_number() - #121801

Merged
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:decimal-from-number
Oct 14, 2024
Merged

gh-121798: Add class method Decimal.from_number()#121801
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:decimal-from-number

Conversation

serhiy-storchaka commented Jul 15, 2024
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Member

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/

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.
Comment thread Lib/test/test_decimal.py Outdated
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
Comment thread Lib/_pydecimal.py
>>> Decimal.from_number(Decimal('3.14')) # another decimal instance
Decimal('3.14')
"""
if isinstance(number, (int, Decimal, float)):

eendebakpt Jul 16, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why this particular selection? We could add Fraction or Rational as well, which is perhaps more in line with #121797 and #121800

Update: the Decimal constructor does not accept Fraction, so the method from_nunber here only accepts types that the constructor can handle

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

But it's possible from the Integral type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The main constructor does not accept the Integral type.

from_number() only supports a subset of values for the main constructor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, then this does make sense.

Comment thread Doc/library/decimal.rst Outdated

.. classmethod:: from_number(number)

Alternative constructor that only accepts numbers (instances of

picnixz Jul 17, 2024
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

rhettinger removed their request for review July 18, 2024 02:42

mdickinson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

+1 for the general idea. I haven't reviewed line-by-line.

Comment thread Lib/_pydecimal.py
>>> Decimal.from_number(Decimal('3.14')) # another decimal instance
Decimal('3.14')
"""
if isinstance(number, (int, Decimal, float)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, then this does make sense.

serhiy-storchaka enabled auto-merge (squash) October 14, 2024 07:35
serhiy-storchaka merged commit 5217328 into python:main Oct 14, 2024
serhiy-storchaka deleted the decimal-from-number branch October 21, 2024 13:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL