Knowledgebase

Common Python Mistakes and How to Spot Them Print

  • python, troubleshooting, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Frequent errors.

INDENTATION PROBLEMS

Mixing tabs and spaces.

Configure the editor for spaces and the problem disappears.

OFF-BY-ONE ERRORS

Positions start at zero, and ranges exclude the end.

MODIFYING A COLLECTION WHILE ITERATING

Produces skipped items or errors.

Iterate over a copy, or build a new collection.

MUTABLE DEFAULT ARGUMENTS

The same object reused across calls.

Use a marker default and create the value inside.

COMPARING WITH THE WRONG OPERATOR

Equality and identity are different checks.

ASSUMING A KEY OR INDEX EXISTS

Check, or access with a default.

CONFUSING A VALUE WITH ITS TEXT REPRESENTATION

Particularly numbers read from files.

FORGETTING A RETURN

A function that computes and returns nothing.

SHADOWING A BUILT-IN NAME

Which then becomes unavailable.

IMPORT ORDERING PROBLEMS

Circular imports, or a file named after a library.

WHAT CATCHES MOST OF THESE

Running frequently, in small pieces.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot