Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jul 15, 2024

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.
>>> Decimal.from_number(Decimal('3.14')) # another decimal instance
Decimal('3.14')
"""
if isinstance(number, (int, Decimal, float)):
Copy link
Contributor

@eendebakpt eendebakpt Jul 16, 2024

Choose a reason for hiding this comment

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

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
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

But it's possible from the Integral type.

Copy link
Member Author

Choose a reason for hiding this comment

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

The main constructor does not accept the Integral type.

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, then this does make sense.


.. classmethod:: from_number(number)

Alternative constructor that only accepts numbers (instances of
Copy link
Member

@picnixz picnixz Jul 17, 2024

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?

Copy link
Member Author

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.

@rhettinger rhettinger removed their request for review July 18, 2024 02:42
Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

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

>>> Decimal.from_number(Decimal('3.14')) # another decimal instance
Decimal('3.14')
"""
if isinstance(number, (int, Decimal, float)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, then this does make sense.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) October 14, 2024 07:35
@serhiy-storchaka serhiy-storchaka merged commit 5217328 into python:main Oct 14, 2024
37 checks passed
@serhiy-storchaka serhiy-storchaka deleted the decimal-from-number branch October 21, 2024 13:34
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