[BangPypers] recursion not working
Vivek Khurana
hiddenharmony at gmail.com
Mon Mar 15 09:44:47 CET 2010
On Mon, Mar 15, 2010 at 1:54 PM, Kenneth Gonsalves <lawgon at au-kbc.org> wrote:
> hi,
>
> here is a function I have written:
>
> def getaccbal(accnt,bal):
> account = Account.objects.get(pk=accnt)
>
> if account.vouch:
> bal = bal + getledgerob(account)
> else:
> children = account.account_set.all()
> for child in children:
> getaccbal(child.id,bal)
> return bal
>
> if I try this with bal=0, on each recursion bal gets reset to 0 and I always
> get a 0 as a result. (If I put bal=20, result is 20). Must be something very
> simple that I am missing.
Look at the else block. you are calling getaccbal but not receiving
the return value in any variable. Unless you receive the value in a
variable or pass bal as a reference, bal will contain the old value.
If you are using OOP then you should be calling geraccbal as self.bal
.
regards
Vivek
--
The hidden harmony is better than the obvious!!
More information about the BangPypers
mailing list