Core Python: exception collection and scope / binding local variable

I have a basic Python "best practice" question. I see that there are already StackOverflow answers regarding this question, but they are bogged down in complex examples or related to several factors.

Given this code:

#!/usr/bin/python def test_function(): try: a = str(5) raise b = str(6) except: print b test_function() 

What is the best way to avoid the inevitable "UnboundLocalError: local variable" b "referenced before the assignment," what am I going to get in the exception handler?

Does python have an elegant way to handle this? If not, what about the inelegant path? In a complex function, I would prefer to avoid checking the existence of each local variable before, for example, I printed debugging information about them.

+4
source share
4 answers

Does python have an elegant way to handle this?

To avoid exceptions when printing unrelated names, the most elegant way is not to print them; the second most elegant is to ensure that the names are associated, for example. linking them at the beginning of the function (for this purpose, placeholder None used along the way).

If not, what about the inelegant path?

 try: print 'b is', b except NameError: print 'b is not bound' 

In a complex function, I would prefer to avoid checking the existence of every local variable before I, for example, debugging information about them

It is also strongly recommended that maintaining your functions is simple (that is, not difficult). As Hoar wrote 30 years ago (in his Turing lecture, β€œImperial Clothing,” reprinted, for example, in this PDF ):

There are two ways to build software design: one way to do it is so simple that there are obviously no flaws, and to make it so complex that there are no obvious flaws. The first method is much more complicated.

Achieving and maintaining simplicity is really difficult: considering that you need to implement a certain general functionality of X, this is the most natural temptation in the world to do this through complex accretion into several complex classes and functions from different bits and parts, β€œsmart” hacks, β€œcopy and insert "and" edit-bit "" disk encoding "etc. etc.

However, it’s worth trying instead to keep your functions β€œso simple that there are obviously no flaws.” If a function is complex for fully unit testing, it is too complex: break it (i.e., reorganize) into its natural components, even if it works to dig them out. (In fact, one of the ways in which to focus on unit testing helps to improve the quality of the code: relentlessly stimulating you so that all the code is fully tested, while at the same time encouraging you to make it simple in its structure).

+8
source

You can initialize your variables outside of the try block

 a = None b = None try: a = str(5) raise b = str(6) except: print b 
+6
source

You can check if a variable is defined in the local scope using the built-in locals() method

http://docs.python.org/library/functions.html#locals

 #!/usr/bin/python def test_function(): try: a = str(5) raise b = str(6) except: if 'b' in locals(): print b test_function() 
+2
source
 def test_function(): try: a = str(5) raise b = str(6) except: print b 

b = str(6) never starts; the program exits the try block immediately after raise . If you want to print some variable in the except block, evaluate it before throwing an exception and putting them in the exception that you throw.

 class MyException(Exception): def __init__(self, var): self.var = var def test_function(): try: a = str(5) b = str(6) raise MyException(b) except MyException,e: print e.var 
+1
source

Source: https://habr.com/ru/post/1308469/


All Articles