`except: pass` Is a Python Anti-Pattern

Real Python · Beginner ·🔧 Backend Engineering ·2mo ago

Key Takeaways

The video discusses the anti-pattern of using `except: pass` in Python, emphasizing the importance of proper error handling and logging in production code.

Full Transcript

touch on one of my peeves, which is except pass. >> Okay. >> I think that should be a compile error. >> Oh, really? >> At bare minimum, you need a comment that says what you're doing there because essentially you're saying, "I would like to ignore an error." And you really should be explaining to somebody why. In production code, it's usually logging something. Even in just partial scripts for yourself, you better put something cuz you come back to it and you're really like, "Oh, well, we'll just ignore it. It's fine." >> [laughter] >> It's almost never fine. There was a reason it accepted in the first place. So, >> Yeah.

Original Description

Download your free Python Cheat Sheet here: https://realpython.com/cheatsheet Free Python Skill Test with instant level + learning plan: https://realpython.com/skill-test Want to learn faster? Become a Python Expert with unlimited access to 5,000+ tutorials, videos, and exercises: https://realpython.com/start Listen to the full episode at https://realpython.com/podcasts/rpp/298 or wherever you get podcasts -- with Chris Trudeau (hosted by Chris Bailey). 🐍 Become a Python expert with real-world tutorials, on-demand courses, interactive quizzes, and 24/7 access to a community of experts at https://realpython.com ▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰ 🐍 Start Here → https://realpython.com/start 🗺️ Guided Learning Paths → https://realpython.com/learning-paths 🎧 Real Python Podcast → https://realpython.com/podcast 📚 Python Books → https://realpython.com/books 📖 Python Reference → https://realpython.com/ref 🧑‍💻 Quizzes & Exercises → https://realpython.com/quizzes 🎓 Live Courses: https://realpython.com/live ⭐️ Reviews & Learner Stories: https://realpython.com/learner-stories ▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
Sign in to unlock AI tutor explanation · ⚡30

The video explains why using `except: pass` is a bad practice in Python and provides guidance on proper error handling and logging.

Key Takeaways
  1. Avoid using `except: pass` in production code
  2. Use logging to track errors
  3. Explain the reason for ignoring an error with a comment
  4. Handle errors properly
💡 Using `except: pass` can mask important errors and make debugging difficult.

Related Reads

Up next
Converting T-SQL code to PL/pgSQL with confidence
Google Cloud Tech
Watch →