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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:27:19+00:00 2026-06-01T03:27:19+00:00

I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,

  • 0

I have a singleton implemented like this:

class Test123(object):        
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Test123, cls).__new__(cls, *args, **kwargs)
        return cls._instance

    def initialize(self):
        self.attr1 = 500
        self.attr2= 0
        self.attr3= 0.10

    def printit(self):
        print self.attr1
        print self.attr2
        print self.attr3

I don;t implement __init__ because it is called every time I use the singleton, so to get around it, I simply call initialize at the start of my script.

Whenever i run it:

Test123().initialize()
time.sleep(1)
Test123().printit()

I get this error:

Traceback (most recent call last):
  File "Z:\test\test123.py", line 22, in <module>
500
    Test123().printit()
  File "Z:\test\test123.py", line 17, in printit
    print self.attr2
AttributeError: 'Test123' object has no attribute 'attr2'

Any ideas what is going on? I am using another singleton and it’s not doing this. Plus, attr1 gets printed fine, I very confused. Might it have something to do with naming, maybe some other singleton has an attribute named attr2?

EDIT: the testcase seems to work fine after I changed repo, so here is the actual code

    import MySQLdb

class DataAccessLayer():
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(DataAccessLayer, cls).__new__(cls, *args, **kwargs)
        return cls._instance

    def initialize(self):
        #init local connection
        self.dalConnection = 0
        try:
            self.dalConnection = MySQLdb.connect('localhost', 'root', 'awesomepassword', 'arb');

        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0],e.args[1])

    def __del__(self):
        self.dalConnection.close()


    def addrow(self):
        try:
            cur = self.dalConnection.cursor()


            cur.close()
            self.dalConnection.commit()

        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0],e.args[1])

DataAccessLayer().initialize()
DataAccessLayer().addrow()

Creates this error:

Traceback (most recent call last):
  File "Z:\test\DataAccess.py", line 36, in <module>
    DataAccessLayer().addrow()
  File "Z:\test\DataAccess.py", line 25, in addOption
    cur = self.dalConnection.cursor()
AttributeError: DataAccessLayer instance has no attribute 'dalConnection'
Exception AttributeError: "DataAccessLayer instance has no attribute 'dalConnection'" in <bound method DataAccessLayer.__del__ of <__main__.DataAccessLayer instance at 0x00000000022A2748>> ignored
  • 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-01T03:27:20+00:00Added an answer on June 1, 2026 at 3:27 am

    Your DataAccessLayer is an old-style class. Try class DataAccessLayer(object): ....

    Update:

    Class Types

    Class types, or “new-style classes,” are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override __new__(). The arguments of the call are passed to __new__() and, in the typical case, to __init__() to initialize the new instance.

    Classic Classes

    Class objects are described below. When a class object is called, a new class instance (also described below) is created and returned. This implies a call to the class’s __init__() method if it has one. Any arguments are passed on to the __init__() method. If there is no __init__() method, the class must be called without arguments.

    source: the python reference

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

Sidebar

Related Questions

So, I've always implemented a singleton like so: class Singleton { private static $_instance
I have a singleton class for global access to config information. This singleton class
I need an object/class that keeps data synchronous over multiple processes. Like a singleton
I have a Logger class which is implemented singleton. The class is simple and
I have a singleton class which I would like to be created using Spring's
I have a class with a constructor that looks like this: public TimedWorker(int timerInterval,
I have a Singleton that is accessed in my class via a static property
I have a singleton object that use another object (not singleton), to require some
Assume I have a singleton class in an external lib to my application. But
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static

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.