Question

I have try-except block in python, and I want to do nothing when exception occurs. My code is as follows:

for i in range(len(grid)):
    for j in range(len(grid[i])):
        try:
            count = count > int(grid[i][j]) ? count : int(grid[i][j])
        except:
            //Do nothing here

How do I do nothing when exception is caught.

Thanks.

Was it helpful?

Solution

pass is the keyword you are looking for.

OTHER TIPS

Let us write the code properly.

  • We want to iterate over each cell of the grid. So do that. Don't create lists of numbers (range) that you iterate over and then use to index back in. Python's for-loops work directly with the container. It is convenient and easy to understand and good. Don't fight that.

  • There is no ?: construct in Python. There is a x if y else z construct, but that is not appropriate here. You are clearly trying to set count to the maximum of its current value and the cell value. There is a built-in function for that.

  • You really want the maximum of all of those cells. So ask for that directly; don't assume that you have to implement the high-water-mark algorithm yourself. You don't. (This also protects you from having to pick an initial value for count, which might be wrong.) We don't need to iterate with an explicit loop for this. We can specify the list of values to pass to max with a generator expression.

  • You want to "ignore" values that can't be converted to integers. Notwithstanding that there probably is something wrong with your other code if the existence of such values could possibly occur in the first place: we can simply make a test, and filter out the values that fail the test.

Thus:

def is_integral(value):
    try:
        int(value)
        return True
    except:
        return False


# Now, get the maximum of all the integral values:
count = max(
    int(cell) for row in grid for cell in row
    if is_integral(cell)
)

You can use pass, but also ... is synonymous in Python 3.x, which can be nice for writing psuedocode.

I see a lot of people use pass where they truly need to do nothing, while using ... where they are using it as a placeholder.

class SomeInterface:
    def do_something(self):
        pass

class SomeImplementation(SomeInterface):
    def do_something(self)
        ...

This makes it easy to search for ... and find areas where you have unimplemented code, without the false positives from pass.

Note that passing on an exception is generally a bad practice, as you will virtually always want to act on exceptions. You should definitely, at the very least, specify the exact exception(s) you want to catch, as otherwise you will catch all exceptions, potentially causing bugs you can't detect if a different exception rears it's head.

for i in range(len(grid)):
    for j in range(len(grid[i])):
        try:
            count = count > int(grid[i][j]) ? count : int(grid[i][j])
        except:
            pass
result = x if a > b else y

Is the ternary operator

The do nothing statement is

pass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top