Generarl programming question.

Chris Angelico rosuav at gmail.com
Sat Apr 11 17:28:28 CEST 2015


On Sun, Apr 12, 2015 at 1:22 AM,  <jonas.thornvall at gmail.com> wrote:
> Thanks i was worried, i try to make a generic base choice algorithm that should work for anybase, and i just realised that the bignumb add would need to call the bignumb subtraction and viceversa. I thought there may be instances but i was not sure.
>
> But I have a feeling the code will be hard to debug.

The thing to watch out for is unbounded recursion, where they might
call each other forever. But you could easily define your two
functions to fall back to the other like this:

def add(x, y):
    if is_negative(y): return subtract(x, -y)
    # implement addition with known-positive y

def subtract(x, y):
    if is_negative(y): return add(x, -y)
    # implement subtraction with known-positive y

There's no problem here, and no possible conflict. (I don't know how
it'd actually help, implementing it like this, but it's certainly
legal.)

ChrisA



More information about the Python-list mailing list