[Tutor] Maximum recursion depth problem.

Wesley Brooks wesbrooks at gmail.com
Tue Aug 3 12:18:36 CEST 2010


Ok a little more investigation has found the follwing work but there
not as tidy. I'd still really appreciate someone explaing why this
behaves like this!

class A:
   def break_down(self, value, base, broken_list=[]):
       power = len(broken_list)
       digit = (value % (base ** (power + 1))) / (base ** power)
       value -= digit * (base**power)
       broken_list.append(digit)
       if value != 0:
           return self.break_down(value, base, broken_list=broken_list)
       else:
           return broken_list

if __name__ == '__main__':
   a = A()
   d_list_1 = a.break_down(34567, 256, [])
   print d_list_1
   a2 = A()
   d_list_2 = a2.break_down(34567, 256, [])
   print d_list_2

......OR:

class A:
   def break_down(self, value, base, broken_list=None):
       if broken_list == None:
           broken_list = []
       power = len(broken_list)
       digit = (value % (base ** (power + 1))) / (base ** power)
       value -= digit * (base**power)
       broken_list.append(digit)
       if value != 0:
           return self.break_down(value, base, broken_list=broken_list)
       else:
           return broken_list

if __name__ == '__main__':
   a = A()
   d_list_1 = a.break_down(34567, 256)
   print d_list_1
   a2 = A()
   d_list_2 = a2.break_down(34567, 256)
   print d_list_2

Yours Faithfully,

Wesley Brooks

On 3 August 2010 11:02, Wesley Brooks <wesbrooks at gmail.com> wrote:
> Dear Python Users,
>
> I'm having real difficulty understanding why the following is not
> working and hoped I've either missed something obvious of I'm doing
> something wrong!
>
> class A:
>    def break_down(self, value, base, broken_list=[]):
>        power = len(broken_list)
>        digit = int((value % (base ** (power + 1))) / (base ** power))
>        value -= digit * (base**power)
>        broken_list.append(digit)
>        if value != 0:
>            return self.break_down(value, base, broken_list=broken_list)
>        else:
>            return broken_list[:]
>
> if __name__ == '__main__':
>    a = A()
>    d_list_1 = a.break_down(34567, 256)
>    print d_list_1
>    a2 = A()
>    d_list_2 = a2.break_down(34567, 256)
>    print d_list_2
>
> When the above runs it fails with the error "RuntimeError: maximum
> recursion depth exceeded while calling a Python object".
>
> The following also does not work:
>
> if __name__ == '__main__':
>    a = A()
>    digit_list_1 = a.break_down(34567, 256)
>    print digit_list_1
>    del digit_list_1, usc
>    a2 = A()
>    digit_list_2 = a2.break_down(34567, 256)
>    print digit_list_2
>
> but the following two do work:
>
> if __name__ == '__main__':
>    a = A()
>    digit_list_1 = a.break_down(34567, 256)
>    print digit_list_1
>    #a2 = A()
>    #digit_list_2 = a2.break_down(34567, 256)
>    #print digit_list_2
>
> if __name__ == '__main__':
>    #a = A()
>    #digit_list_1 = a.break_down(34567, 256)
>    #print digit_list_1
>    a2 = A()
>    digit_list_2 = a2.break_down(34567, 256)
>    print digit_list_2
>
> I'm a little stumped as I don't think I'm using any global or class
> variables? Any help would be much appreciated.
>
> Yours Faithfully,
>
> Wesley Brooks
>


More information about the Tutor mailing list