Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:47:41+00:00 2026-06-10T20:47:41+00:00

I’m not asking for personal religious opinions about this philosophy, rather something a bit

  • 0

I’m not asking for personal “religious” opinions about this philosophy, rather something a bit more technical.

I understand this phrase is one of several litmus tests to see if your code is “pythonic”. But to me, pythonic means clean, simple and intuitive, not loaded with exception handlers for bad coding.

So, practical example. I define a class:

class foo(object):
    bar = None

    def __init__(self):
        # a million lines of code
        self.bar = "Spike is my favorite vampire."
        # a million more lines of code

Now, coming from a procedural background, in another function I wanna do this:

if foo.bar:
    # do stuff

I’ll get an attribute exception if I was impatient and did not do the initial foo = None. So, “ask forgiveness not permission” suggests I should do this instead?

try:
    if foo.bar:
        # do stuff
except:
    # this runs because my other code was sloppy?

Why would it be better for me to add additional logic in a try block just so I can leave my class definition more ambiguous? Why not define everything initially, therfore explicitly grant permission?

(Don’t beat me up about using try/except blocks… I use them everywhere. I just don’t think it’s right to use them to catch my own errors because I wasn’t a thorough programmer.)

Or… do I completely misunderstand the “Ask Forgivess” mantra?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:47:42+00:00Added an answer on June 10, 2026 at 8:47 pm

    The classical “ask forgiveness not permission” example is accessing values from a dict that may not exist. E.g.:

    names = { 'joe': 'Joe Nathan', 'jo': 'Jo Mama', 'joy': 'Joy Full' }
    name = 'hikaru'
    
    try:
        print names[name]
    except KeyError:
        print "Sorry, don't know this '{}' person".format(name)
    

    Here the exception that might occur (KeyError) is stated, so that you’re not asking forgiveness for every error that might occur, but only the one that would naturally occur.
    For comparison, the “ask permission first” approach might look like:

    if name in names:
        print names[name]
    else:
        print "Sorry, don't know this '{}' person".format(name)
    

    or

    real_name = names.get(name, None)
    if real_name:
        print real_name
    else:
        print "Sorry, don't know this '{}' person".format(name)
    

    Such examples of “ask forgiveness” are often too simple. IMO it’s not crystal clear that try/except blocks are inherently better than if/else. The real value is much clearer when performing operations that might fail in a variety of ways–such as parsing; using eval(); accessing operating system, middleware, database, or network resources; or performing complex mathematics. When there are multiple potential failure modes, being prepared to get forgiveness is hugely valuable.

    Other notes about your code examples:

    You do not need to ladle try/except blocks around every variable usage. That would be horrible. And you don’t need to set self.bar in your __init__() since it’s set in your class definition above. It is usual to define it either in the class (if it’s data likely to be shared among all instances of the class) or in the __init__() (if it’s instance data, specific to each instance).

    A value of None is not undefined, or an error, by the way. It’s a specific and legitimate value, meaning none, nil, null, or nothing. Many languages have such values so programmers don’t “overload” 0, -1, '' (empty string) or similar useful values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.