Home >Backend Development >Python Tutorial >Ask Forgiveness, Not Permission: When Is This Programming Philosophy Actually Beneficial?
"Ask Forgiveness Not Permission" - Technical Explanation
The philosophy of "asking forgiveness not permission" in programming refers to the practice of attempting an operation and handling any resulting errors, rather than explicitly checking for permission beforehand. However, despite its pythonic nature, this approach has its limitations.
When dealing with optional object attributes, as in the example of the foo class, it is generally not considered a failure of the object if a certain attribute is absent. Instead, it is often indicative of a programming error. Therefore, a better approach is to explicitly initialize optional attributes to None and then check their presence using if foo.bar is not None.
In the context of "ask forgiveness not permission," the reason for avoiding exception handling in such cases is that it hides the true cause of the error, which is likely a programming mistake. Instead, it is preferable to explicitly define initial values for optional attributes, ensuring that the code's behavior is clear and predictable.
In summary, while "ask forgiveness not permission" may be appropriate in certain situations, it is not universally applicable. When dealing with optional object attributes, it is more beneficial to explicit define default values and check for their presence rather than relying on exception handling to accommodate potential errors.
The above is the detailed content of Ask Forgiveness, Not Permission: When Is This Programming Philosophy Actually Beneficial?. For more information, please follow other related articles on the PHP Chinese website!