سؤال

Is there some way I could make decimal.Decimal the default type for all numerical values in Python? I would like to be able to use Python in a manner similar to the bc and dc programs without having to call decimal.Decimal(...) for every number.

EDIT: For the uninitiated: bc.

EDIT 2: Thank you tokenize module..

هل كانت مفيدة؟

المحلول

At the bottom of the tokenize module's documentation, there is a function that does exactly what I need:

نصائح أخرى

You cannot really do what you ask without some serious magic, which I won't try to touch upon in my answer, but there is at least a slightly easier way than doing decimal.Decimal(...)

from decimal import Decimal as D
num = D("1") + D("2.3")

Edit: use the shorter form from the comment.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top