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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:44:39+00:00 2026-05-10T19:44:39+00:00

I regularly want to check if an object has a member or not. An

  • 0

I regularly want to check if an object has a member or not. An example is the creation of a singleton in a function. For that purpose, you can use hasattr like this:

class Foo(object):     @classmethod     def singleton(self):         if not hasattr(self, 'instance'):             self.instance = Foo()         return self.instance 

But you can also do this:

class Foo(object):     @classmethod     def singleton(self):         try:             return self.instance         except AttributeError:             self.instance = Foo()             return self.instance 

Is one method better of the other?

Edit: Added the @classmethod … But note that the question is not about how to make a singleton but how to check the presence of a member in an object.

Edit: For that example, a typical usage would be:

s = Foo.singleton() 

Then s is an object of type Foo, the same each time. And, typically, the method is called many times.

  • 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. 2026-05-10T19:44:39+00:00Added an answer on May 10, 2026 at 7:44 pm

    These are two different methodologies: №1 is LBYL (look before you leap) and №2 is EAFP (easier to ask forgiveness than permission).

    Pythonistas typically suggest that EAFP is better, with arguments in style of ‘what if a process creates the file between the time you test for it and the time you try to create it yourself?’. This argument does not apply here, but it’s the general idea. Exceptions should not be treated as too exceptional.

    Performance-wise in your case —since setting up exception managers (the try keyword) is very cheap in CPython while creating an exception (the raise keyword and internal exception creation) is what is relatively expensive— using method №2 the exception would be raised only once; afterwards, you just use the property.

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

Sidebar

Related Questions

Can PHP dissect its own syntax? For example, I'd like to write a function
For a given URL, I want to check if the content has changed since
I want to check if my string holds following value or not: For samples:
I have a string and I want to check that it only consists of
I want to check a name using regular expression, but I'm not getting convincing
I want a regular expression that validate a string that allow only numbers and
i want a regular expression that contains alpha and numeric characters and must be
I want to deploy a PSGI scripts that runs in Apache2 with Plack. Apache
How can I access a user session objects from another thread? I want to
I have got a method that takes a long time to complete and want

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.