MCPcopy Index your code
hub / github.com/RustPython/RustPython / __new__

Method __new__

Lib/_pydecimal.py:458–606  ·  view source on GitHub ↗

Create a decimal point instance. >>> Decimal('3.14') # string input Decimal('3.14') >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) Decimal('3.14') >>> Decimal(314) # int Decimal('314') >>> D

(cls, value="0", context=None)

Source from the content-addressed store, hash-verified

456
457 # We're immutable, so use __new__ not __init__
458 def __new__(cls, value="0", context=None):
459 """Create a decimal point instance.
460
461 >>> Decimal('3.14') # string input
462 Decimal('3.14')
463 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
464 Decimal('3.14')
465 >>> Decimal(314) # int
466 Decimal('314')
467 >>> Decimal(Decimal(314)) # another decimal instance
468 Decimal('314')
469 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
470 Decimal('3.14')
471 """
472
473 # Note that the coefficient, self._int, is actually stored as
474 # a string rather than as a tuple of digits. This speeds up
475 # the "digits to integer" and "integer to digits" conversions
476 # that are used in almost every arithmetic operation on
477 # Decimals. This is an internal detail: the as_tuple function
478 # and the Decimal constructor still deal with tuples of
479 # digits.
480
481 self = object.__new__(cls)
482
483 # From a string
484 # REs insist on real strings, so we can too.
485 if isinstance(value, str):
486 m = _parser(value.strip().replace("_", ""))
487 if m is None:
488 if context is None:
489 context = getcontext()
490 return context._raise_error(ConversionSyntax,
491 "Invalid literal for Decimal: %r" % value)
492
493 if m.group('sign') == "-":
494 self._sign = 1
495 else:
496 self._sign = 0
497 intpart = m.group('int')
498 if intpart is not None:
499 # finite number
500 fracpart = m.group('frac') or ''
501 exp = int(m.group('exp') or '0')
502 self._int = str(int(intpart+fracpart))
503 self._exp = exp - len(fracpart)
504 self._is_special = False
505 else:
506 diag = m.group('diag')
507 if diag is not None:
508 # NaN
509 self._int = str(int(diag or '0')).lstrip('0')
510 if m.group('signal'):
511 self._exp = 'N'
512 else:
513 self._exp = 'n'
514 else:
515 # infinity

Callers 1

_dec_from_tripleFunction · 0.45

Calls 13

isinstanceFunction · 0.85
getcontextFunction · 0.85
strFunction · 0.85
lenFunction · 0.85
_raise_errorMethod · 0.80
absFunction · 0.70
replaceMethod · 0.45
stripMethod · 0.45
groupMethod · 0.45
lstripMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected