0

I have a file num.py in which I have defined a variable as

import decimal

fivedecimal = Decimal(10) ** -5

This particular variable is being used in other python files as:

import decimal

from num import fivedecimal

b= decimal.Decimal(100.05).quantize(fivedecimal)

Now the problem I am facing is that I have to add a parameter for rounding=ROUND_CEILING but I have to do that in the first file only as the variable fivedecimal is being used in multiple files in different contexts for quantizing values to five decimal places, but I want to make a change in only the first file so that I don't have to make changes in the multiple files where I am importing fivedecimal.

Basically my question is if there is a way in which I can specify the two parameters as a variable in the first file (num.py) that is fivedecimal = (Decimal(10)** -5, rounding=ROUND_CEILING) and then automatically wherever I am using the fivedecimal variable the decimals are being rounded off as this will save me a lot of time as there are more than 250 places where I am using the fivedecimal variable and if this is not possible then I will have to make the changes in all the 250 places by adding an extra parameter rounding=ROUND_CEILING i.e. quantize(fivedecimal, rounding=ROUND_CEILING).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The proper fix would probably be to subclass `decimal` to implement this additional behavior. Are you familiar with classes and inheritance? – tripleee Feb 05 '23 at 10:26
  • Can you please elaborate a little....are you saying that I have to create a subclass of decimal class and implement this behavior, if yes then how should I be doing it, please help. – shubham_lv1 Feb 05 '23 at 10:38
  • Something like that; but it also depends on how exactly the variable is being used. If you always call `quantize` on it, wrapping this method could work instead, or as well. – tripleee Feb 05 '23 at 11:29

0 Answers0