I have defined a class to contain a grid of points. I want to perform error checking on the keyword argument Grid_Points
to make sure the user gives either a float or int for the number of points. I want there to be an error if they don't specify anything.
class MyGrid:
def __init__(self, Grid_Points=None, L=0.0, R=1.0):
Grid = np.linspace(start=L, stop=R,num=Grid_Points, retstep=True)
self.Grid = Grid[0]
self.dx = Grid[1]
I implemented the following try
and except
clauses. When I do TestGrid = MyGrid()
I get an error that says UnboundLocalError: local variable 'Grid' referenced before assignment
.
What am I missing? I thought trying to do linspace
within the try
clause would result in an exception (because Grid_Points
will be equal to None
) and so it should go to the except clause and print out the statement I specified and then terminate execution of the code. I purposely chose to use Exception
so it would catch anything (I'll use something more specific once I get this working at all). But the code appears to be getting past the try
and except
blocks.
class MyGrid:
def __init__(self, Grid_Points=None, L=0.0, R=1.0):
try:
Grid = np.linspace(start=L, stop=R,num=Grid_Points, retstep=True)
except Exception:
print('Enter the number of grid points as either a float or int')
self.Grid = Grid[0]
self.dx = Grid[1]