Home > Backend Development > Python Tutorial > Ask Forgiveness, Not Permission: When Is Exception Handling Better Than Preemptive Checks?

Ask Forgiveness, Not Permission: When Is Exception Handling Better Than Preemptive Checks?

Linda Hamilton
Release: 2024-12-30 21:59:14
Original
334 people have browsed it

Ask Forgiveness, Not Permission: When Is Exception Handling Better Than Preemptive Checks?

"Ask Forgiveness Not Permission": A Technical Explanation

In programming, the phrase "ask forgiveness not permission" describes two contrasting coding styles:

"Ask for Permission" Style:

if can_do_operation():
    perform_operation()
else:
    handle_error_case()
Copy after login

"Ask for Forgiveness" Style:

try:
    perform_operation()
except Unable_to_perform:
    handle_error_case()
Copy after login

In the "ask for permission" approach, the presence of the can_do_operation() check prevents the perform_operation() call from executing if the operation cannot be performed. However, this approach relies on the accuracy of the can_do_operation() check, which may not always be reliable in dynamic environments or when dealing with external resources.

Benefits of "Ask for Forgiveness"

The "ask for forgiveness" style offers several benefits:

  • Handles Concurrent Situations: In multithreaded or multi-resource environments, the conditions may change between the check and the operation, making the "ask for permission" approach ineffective.
  • Avoids Overly Restrictive Checks: "Ask for permission" requires defining precise and comprehensive criteria, which can be challenging and error-prone. The "ask for forgiveness" approach allows for more flexibility and avoids the unintentional exclusion of valid operations.

Example: Instance Attribute Access

In your example, you inquire about the use of "ask for forgiveness" when accessing instance attributes. While normally considered a programmer error, accessing a non-existent attribute can be a valid scenario, such as when dealing with optional object parts.

Instead of testing for the existence of an attribute (foo.bar) with an exception handler, it's more Pythonic to check if the attribute is not None. For optional attributes, the bar attribute is typically initialized to None initially and set to a meaningful value if available. This allows for the following test:

if foo.bar is not None:
    handle_optional_part(foo.bar)
else:
    default_handling()
Copy after login

Conclusion

The "ask forgiveness not permission" principle recommends embracing exceptions as a natural part of program execution, particularly when dealing with optional functionality or external resource interactions. It provides greater flexibility and adaptability in dynamic and concurrent environments, while still enabling error handling through exception handling mechanisms.

The above is the detailed content of Ask Forgiveness, Not Permission: When Is Exception Handling Better Than Preemptive Checks?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template