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

  • Home
  • SEARCH
  • 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 1085735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:43:34+00:00 2026-05-16T22:43:34+00:00

In several places I have to retrieve some value from a dict, but need

  • 0

In several places I have to retrieve some value from a dict, but need to check if the key for that value exists, and if it doesn’t I use some default value :

    if self.data and self.data.has_key('key'):
        value = self.data['key']
    else:
        value = self.default
    ....

One thing I like about python, is that and/or boolean operators return one of their operands. I’m not sure, but if exceptions evaluated to false, the above code could be rewriten as follows:

    value = self.data['key'] or self.default

I think its intuitive that errors should evaluate to false(like in bash programming). Is there any special reason for python treating exceptions as true?

EDIT:

I just want to show my point of view on the ‘exception handling’ subject. From wikipedia:

”
From the point of view of the author of a routine, raising an exception is a useful way to signal that a routine could not execute normally. For example, when an input argument is invalid (e.g. a zero denominator in division) or when a resource it relies on is unavailable (like a missing file, or a hard disk error). In systems without exceptions, routines would need to return some special error code. However, this is sometimes complicated by the semipredicate problem, in which users of the routine need to write extra code to distinguish normal return values from erroneous ones.
“

As I said, raising exceptions are just a fancy way of returning from a function. ‘Exception objects’ are just a pointer to a data structure that contains information about the error and how that is handled on high level languages depend only on the implementation of the VM. I decided to look at how python 2.6.6 deals with exceptions by looking at output from the ‘dis’ module:

>>> import dis
>>> def raise_exception():
...     raise Exception('some error')
... 
>>> dis.dis(raise_exception)
  2           0 LOAD_GLOBAL              0 (Exception)
              3 LOAD_CONST               1 ('some error')
              6 CALL_FUNCTION            1
              9 RAISE_VARARGS            1
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE        

Its clear that python has the exception concept at bytecode level, but even if it didnt, it could still make exception handling constructs behave like they do. Take the following function:

def raise_exception():
...     return Exception('some error')
... 
>>> dis.dis(raise_exception)
  2           0 LOAD_GLOBAL              0 (Exception)
              3 LOAD_CONST               1 ('some error')
              6 CALL_FUNCTION            1
              9 RETURN_VALUE

Both functions create the exception object in the same way as show in 0, 3 and 6. The difference is that the first call the RAISE_VARARGS instruction on the object(and stills returns None) and the second will just return the exception object to calling code. Now take the following bytecode(I’m not sure if this is correct) :

0  LOAD_GLOBAL      (isinstance) #Loads the function 'isinstance'
3  LOAD_GLOBAL      (raise_exception) #loads the function 'raise_exception'
6  CALL_FUNCTION    #call the function raise_exception(which will push an Exception instance to the stack
9  LOAD_GLOBAL      (Exception) #load the Exception class
12 CALL_FUNCTION    #call the 'isinstance function'(which will push 'True to the stack)
15 JUMP_IF_TRUE     (to 27) #Will jump to instruction 33 if the object at the top of stack evaluates to true
18 LOAD_CONS        ('No exceptions were raised')
21 PRINT_ITEM
24 PRINT_NEWLINE    
27 LOAD_CONS        ('Exception caught!')
21 PRINT_ITEM
24 PRINT_NEWLINE

The above translates to something equivalent to this:

if isinstance(raise_exception(), Exception):
    print 'Exception caught!'
else:
    print 'No exceptions were raised'

However, the compiler could generate something like the above instructions when it finds a try block. With this implementation someone could either test a block for an exception or treat functions that return an exception as a ‘False’ value.

  • 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-05-16T22:43:34+00:00Added an answer on May 16, 2026 at 10:43 pm

    Why do exceptions/errors evaluate to True in python?

    Instances of Exception evaluate to True (EDIT See @THC4k‘s comment below. Quite relevant information). That doesn’t prevent them from being thrown.

    I’m not sure, but if exceptions evaluated to false

    In your context it wouldn’t suffice that Exceptions evaluate to False. They should also not get thrown and be propagated down the call stack. Rather, they will have to be “stopped” on the spot and then evaluate to False.

    I’ll leave it to more experienced Pythonistas to comment on why Exceptions do not (or should not) get “stopped” and evaluate to True or False. I’d guess that this is because they are expected to be thrown and propagated. In fact they would cease to be “exceptions” if they were to be stopped and interrogated =P.

    but need to check if the key for that value exists, and if it doesn’t I use some default value

    I can think of two options two ways to get a default value in the absence of a key in a dictionary. One is to use defaultdict. The other is to use the get method.

    >>> from collections import defaultdict
    >>> d = defaultdict(lambda: "default")
    >>> d['key']
    'default'
    
    >>> d = dict()
    >>> d.get('key', 'default')
    'default'
    >>> 
    

    PS: if key in dict is preferred to dict.has_key(key). In fact has_key() has been removed in Python 3.0.

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

Sidebar

Related Questions

We have a controller that might be pushed from several places, so its back
I have an application that uses AJAX liberally. I have several places where a
I have read in several places that it's possible to share the objects directory
I have a ListView template that's being used in several places and I'd like
There are several places on the internet that talk about having multy key dictionary
I have several places where I need to display an alert and handle the
I'm find I have several places that having public static inner classes designed that
Scenario: I have a partial view that is used in several places across my
I have data stored in a sqlite3 database and have several places that I
I have read in a several places (e.g. here ) that I can use

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.