Python Try-Except inside of Function

后端 未结 6 1903
旧巷少年郎
旧巷少年郎 2020-12-11 23:03

I\'ve got a pretty good understanding of python\'s try-except clause, but I\'m encountering problems when trying to put it inside of a function.

>>>         


        
6条回答
  •  死守一世寂寞
    2020-12-11 23:44

    The name error is happening before it ever gets into tryAppend. It evaluates the value of foo when trying to pass it to the function. This works:

    def tryAppend(child, parent):
        parent.append(child)
    
    var1 = []
    try:
        tryAppend(foo, var1)
    except NameError:
        print 'WRONG NAME'
    

提交回复
热议问题