I wrote a class that can handle integers with arbitrary precision (just for learning purposes). The class takes a string representation of an integer and converts it into an instance of BigInt
for further calculations.
Often times you need the numbers Zero and One, so I thought it would be helpfull if the class could return these. I tried the following:
class BigInt():
zero = BigInt("0")
def __init__(self, value):
####yada-yada####
This doesn't work. Error: "name 'BigInt' is not defined"
Then I tried the following:
class BigInt():
__zero = None
@staticmethod
def zero():
if BigInt.__zero is None:
BigInt.__zero = BigInt('0')
return BigInt.__zero
def __init__(self, value):
####yada-yada####
This actually works very well. What I don't like is that zero
is a method (and thus has to be called with BigInt.zero()
) which is counterintuitive since it should just refer to a fixed value.
So I tried changing zero
to become a property, but then writing BigInt.zero
returns an instance of the class property
instead of BigInt
because of the decorator used. That instance cannot be used for calculations because of the wrong type.
Is there a way around this issue?