Ad

"Ask for forgiveness, not permission"-style. This is about 40% faster in the general case.

Code
Diff
  • def division(a, b):
        try:
            return a / b
        except:
            return None
    • def division(a, b):
    • if b == 0:
    • return None
    • return a / b
    • try:
    • return a / b
    • except:
    • return None

Refactor to one-liner.

Code
Diff
  • def division(a, b):
        return a / b if b != 0 else None
    
    • def division(a, b):
    • if b == 0:
    • return None
    • return a / b
    • return a / b if b != 0 else None