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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:26:09+00:00 2026-06-13T11:26:09+00:00

I have read this question but it isn’t clear to me. I defined my

  • 0

I have read this question but it isn’t clear to me. I defined my class like this:

from sqlite3 import Connection, Cursor, Row, connect

class myclass(object):
    def __init__(self,number):
        co = connect('C:\\mydatabase.sqlite')
        co.row_factory = Row
        with connection:            
            cu = connection.cursor()
            sql = '''SELECT * FROM mytable WHERE Number= {n} LIMIT 1'''.format(n = number)
            cu.execute(sql)
            for i in cu:
                self.classattribute1 = i['Field1']
                self.classattribute2 = i['Field2']
                etc.

Now this works fine until I want to add a third attribute to my class like:

self.classattribute3 = self.classattribute1 + self.classattribute2
AttributeError: 'myclass' object has no attribute 'classattribute1'

This won’t work if the SELECT statement didn’t return anything, if the number isn’t in the database.

Now what I would like to do when I call an instance of myclass like:

myclassinstance1 = myclass(100)

I would like to write something like:

if cu.fetchone() == None:
    #code to exit the method __init__ and to delete my class instance here

I don’t know how to exit and delete the instance I called from inside myclass. I need to delete theses instances because I don’t want to use empty class instances.

Thanks for reading.

  • 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-13T11:26:10+00:00Added an answer on June 13, 2026 at 11:26 am

    Just create a factory function that will either return a new instance or None if if couldn’t be loaded:

    class MyClass(object):
        def __init__(self, attribute1, attribute2, ...):
            self.attribute1 = attribute1
            self.attribute2 = attribute2
            # ...
    
        @staticmethod
        def load_from_db(number):
            # set up and query database
            record = cursor.fetchone()
            if record == None:
                return None
            else:
                return MyClass(record['Field1'], record['Field2'], ...)
    

    Then load MyClass object from a database with:

    my_obj = MyClass.load_from_db(number)
    

    You can’t delete an object in Python (from __init__ or anywhere), you can only delete a single reference to an object from a scope that contains this reference. (E.g. a scope calling MyClass(), like the load_from_db() function in the code above.)

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

Sidebar

Related Questions

I have read this question and the simple and clear answer but it's not
I have read this question but it's not quite what I was looking for.
I'm hoping this isn't just duplicating this question . I have read the documentation
Hi I know this question have been asked before but the answers there isn't
I have seen this question asked previously but can not find a clear explanation
I have read several questions regarding this but I fear they may be out
I have read numerous people with this question and have tried numerous strategies. However,
I have read some different responses for this same question, so I dont know
I was reading this question (which you do not have to read because I
I have searched here, GooBingHooVista'd the world and read this related question for VS

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.