简体   繁体   中英

python return to try block after expect exception

I have the Decorators traced for some recursive function. I want to know the way to return to the try block. I have tried the while loop, but it does not work for me because my function is recursive, can anyone give me an idea how to deal with that? the problems is when the function change_t raise an exception, i want to continue excute my try block

here is my Decorators and function

proper result:

,- change_t([9, 7, 5], 44)
| ,- change_t([9, 7, 5], 35)
| | ,- change_t([9, 7, 5], 26)
| | | ,- change_t([9, 7, 5], 17)
| | | | ,- change_t([9, 7, 5], 8)
| | | | | ,- change_t([7, 5], 8)
| | | | | | ,- change_t([7, 5], 1)
| | | | | | | ,- change_t([5], 1)
| | | | | | | | ,- change_t([], 1)
| | | | | | ,- change_t([5], 8)
| | | | | | | ,- change_t([5], 3)
| | | | | | | | ,- change_t([], 3)
| | | | | | | ,- change_t([], 8)
| | | | ,- change_t([7, 5], 17)
| | | | | ,- change_t([7, 5], 10)
| | | | | | ,- change_t([7, 5], 3)
| | | | | | | ,- change_t([5], 3)
| | | | | | | | ,- change_t([], 3)
| | | | | | ,- change_t([5], 10)
| | | | | | | ,- change_t([5], 5)
| | | | | | | | ,- change_t([5], 0)
| | | | | | | | `- []
| | | | | | | `- [5]
| | | | | | `- [5, 5]
| | | | | `- [5, 5]
| | | | `- [7, 5, 5]
| | | `- [7, 5, 5]
| | `- [9, 7, 5, 5]
| `- [9, 9, 7, 5, 5]
`- [9, 9, 9, 7, 5, 5]

this is what i got: it stop right after I expect an exception

change_t([9, 7, 5], 44)
,- change_t ([9, 7, 5], 44)
| ,- change_t ([9, 7, 5], 35)
| | ,- change_t ([9, 7, 5], 26)
| | | ,- change_t ([9, 7, 5], 17)
| | | | ,- change_t ([9, 7, 5], 8)
| | | | | ,- change_t ([7, 5], 8)
| | | | | | ,- change_t ([7, 5], 1)
| | | | | | | ,- change_t ([5], 1)
| | | | | | | | ,- change_t ([], 1)
| | | | | | `- 1
| | | | | `- 1
| | | | `- 1
| `- 8
`- 8
`- 17
`- 26
`- 35
`- 44
44

As I said in the comments, you need to reraise the exception so that the original function can actually catch it and continue. You want to decrease the indent only by one within the except handler, so that you don't end up too far on the left:

class traced(object):
    indent =0
    def __init__(self,f):
        self.__name__=f.__name__
        self.indent=0
        self.f=f         
    def __call__(self,*args,**kwargs):
        string=""           
        if kwargs:
           l=[]
           for (key, value) in kwargs.items():
               l.append(str(key) + "=" + str(value))
           a=', '.join(l)
           string = '('+a+')'              
        else:
             l=[]
             for value in args:
                 l.append(str(value))
             a=', '.join(l)
             string = '('+a+')'       
        print('| ' * traced.indent + ',- '+ self.__name__+' '+string)   
        try:
            traced.indent+=1
            value = self.f(*args,**kwargs)                
        except Exception:
            traced.indent-=1  # <-- only decrement by one
            raise             # <-- reraise the exception so the original function can catch it
        traced.indent-=1
        print('| '* traced.indent + "`- "+ repr(value))          
        return value

And then it works:

>>> change_t([9, 7, 5], 44)
,- change_t ([9, 7, 5], 44)
| ,- change_t ([9, 7, 5], 35)
| | ,- change_t ([9, 7, 5], 26)
| | | ,- change_t ([9, 7, 5], 17)
| | | | ,- change_t ([9, 7, 5], 8)
| | | | | ,- change_t ([7, 5], 8)
| | | | | | ,- change_t ([7, 5], 1)
| | | | | | | ,- change_t ([5], 1)
| | | | | | | | ,- change_t ([], 1)
| | | | | | ,- change_t ([5], 8)
| | | | | | | ,- change_t ([5], 3)
| | | | | | | | ,- change_t ([], 3)
| | | | | | | ,- change_t ([], 8)
| | | | ,- change_t ([7, 5], 17)
| | | | | ,- change_t ([7, 5], 10)
| | | | | | ,- change_t ([7, 5], 3)
| | | | | | | ,- change_t ([5], 3)
| | | | | | | | ,- change_t ([], 3)
| | | | | | ,- change_t ([5], 10)
| | | | | | | ,- change_t ([5], 5)
| | | | | | | | ,- change_t ([5], 0)
| | | | | | | | `- []
| | | | | | | `- [5]
| | | | | | `- [5, 5]
| | | | | `- [5, 5]
| | | | `- [7, 5, 5]
| | | `- [7, 5, 5]
| | `- [9, 7, 5, 5]
| `- [9, 9, 7, 5, 5]
`- [9, 9, 9, 7, 5, 5]
[9, 9, 9, 7, 5, 5]

And finally I would clean up the decorator a bit to make it more concise and clear what you are doing:

class traced(object):
    indent = 0

    def __init__(self, f):
        self.__name__ = f.__name__
        self.f = f

    def __call__(self, *args, **kwargs):
        if kwargs:
            l = [str(key) + '=' + str(value) for key, value in kwargs.items()]
        else:
            l = list(map(str, args))
        print('| ' * traced.indent + ',- {0} ({1})'.format(self.__name__, ', '.join(l)))
        try:
            traced.indent += 1
            value = self.f(*args,**kwargs)                
        finally:
            traced.indent -= 1

        print('| ' * traced.indent + '`- ' + repr(value))
        return value

Here I simplified the whole argument aggregation to use list comprehension. Also I used string formatting to make the format a bit clearer. That way you can also combine the parentheses you had to put around the list content (which you did in both cases btw.). And as you reraise the exception without really looking at it, we don't need to catch it to begin with but just make sure that we adjust the indentation in the finally block.

And actually, why check for either variable arguments or keyword arguments? Just accept both:

l = list(map(str, args))
l.extend([str(key) + '=' + str(value) for key, value in kwargs.items()])

When an exception is raised, control is relinquished from the code from which it was raised. It is instead handed over to the first catch block that catches that exception, or to the main interpreter loop.

Take a look at your code:

if a==0:
    return []
elif len(l)==0:
    raise ChangeException()
elif l[0]>a:
    return change_t(l[1:],a)

What happens when the third condition is true, ie l[0]>a , when the length of l is 1? Then on the next call, ie, return change_t(l[1:],a) it will raise an exception which will not be caught by anyone except the main loop: that is why your code fails. You need to wrap that third condition in a try catch clause, depending on what you want to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM