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 657455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:49:33+00:00 2026-05-13T22:49:33+00:00

try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that

  • 0
try:
    spam.foo
except AttributeError:
    do_somthing()

(Is it wise to check an attribute like that without using it?)

  • 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-13T22:49:33+00:00Added an answer on May 13, 2026 at 10:49 pm

    Update:

    If you are really only interested in whether the attribute foo exists (and not doing something with the attribute) than of course hasattr() might be better way to check for the attribute.
    From a developer/user point of view I have to confess that, for me, the use of hasattr() better reflects your intention. And besides that it would result in less code:

    if not hasattr(spam,'foo'):
        do_something()
    

    The documentation of hasattr() describes that it is implemented by:

    (This is implemented by) calling getattr(object, name) and seeing whether it raises an exception or not.

    So basically hasattr() does exactly the same as you are doing. In this case I would definitely go with the build in solution, i.e. hasattr(). You won’t gain any speed advantage.


    Python encourages the EAFP paradigm:

    It is Easier to Ask for Forgiveness than Permission

    So yes this is exactly the way one should do this if you more or less know that spam will have a foo attribute most of the time (the code will be (little?) faster).
    Otherwise, if spam does not have a foo attribute (most of the time), then this approach would be better:

    if hasattr(spam, 'foo'):
        bar = spam.foo
    else:
        do_somthing()
    

    The section on Wikipedia I linked to describes this. Quote (where the EAFP version refers to the way you wrote your code sample):

    These two code samples have the same effect, although there will be performance differences. When spam has the attribute eggs, the EAFP sample will run faster. When spam does not have the attribute eggs (the “exceptional” case), the EAFP sample will run slower. (…) If exceptional cases are rare, then the EAFP version will have superior average performance than the alternative.

    Which is somehow obvious as (with EAFP) you don’t have to introspect the object every time before you want to access the attribute.

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

Sidebar

Ask A Question

Stats

  • Questions 338k
  • Answers 338k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I suggest you take a look at "Named scopes are… May 14, 2026 at 4:20 am
  • Editorial Team
    Editorial Team added an answer I would split the URLs based on their path separator… May 14, 2026 at 4:20 am
  • Editorial Team
    Editorial Team added an answer Apply the definition of intersection from set theory. So if… May 14, 2026 at 4:19 am

Related Questions

I need to flatten objects into nested dicts of the object's properties. The objects
My application has a button to execute a python script dynamically using execfile .
I have some a list comprehension in Python in which each iteration can throw
for the purpose of preventing spam in my application, i wanted to try Akismet
What I am trying to do is pass the raw content of an outgoing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.