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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:57:45+00:00 2026-06-18T03:57:45+00:00

I have my class template here: import sqlite3 class Patron(object): #Let’s set some basic

  • 0

I have my class template here:

 import sqlite3

class Patron(object):
    #Let's set some basic attributes
    attributes = { "patron_id" : None,
    "name" : None,
    "address" : None,
    "phone" : None,
    "email" : None,
    "fee_balance" : None,
    "fees_per_day" : None,
    "books_checked_out" : [],
    "books_overdue" : []}

    def __init__(self):
        #Create a empty instance
        pass

    def new(self, patron_id, name, address, phone, email):
        #Create an instance with new values
        self.attributes["patron_id"] = patron_id
        self.attributes["name"] = name
        self.attributes["address"] = address
        self.attributes["phone"] = phone
        self.attributes["email"] = email

    def retrieve(self, patron_id):
        #Connect to database and prepare patron id
        self.attributes["patron_id"] = patron_id
        patron_database = sqlite3.connect('patrons.db')
        cursor = patron_database.cursor()
        t = (str(patron_id),)

        #Get data from database
        cursor.execute("SELECT * FROM patrons WHERE id =?", t)
        data = cursor.fetchone()

        #Now close your database connection
        patron_database.close()

        #Parse tuple into attributes
        self.attributes["name"] = data[1]
        self.attributes["address"] = data[2]
        self.attributes["phone"] = data[3]
        self.attributes["email"] = data[4]
        self.attributes["fee_balance"] = data[5]
        self.attributes["fees_per_day"] = data[6]
        self.attributes["books_checked_out"] = data[7]
        self.attributes["books_overdue"] = data[8]

    def save(self):
        #Connect to the database
        patron_database = sqlite3.connect('patrons.db')
        cursor = patron_database.cursor()

        #Compile the data into a list
        attributes = []
        for value in self.attributes.itervalues():
            attributes.append(value)

        #Insert the values and save them
        cursor.execute("INSERT INTO patrons VALUES(?,?,?,?,?,?,?,?,?)", attributes)
        patron_database.commit()

        #Close the connection
        patron_database.close()

and then I have my test code here:

'''
Created on Feb 2, 2013

@author: Zach
'''
from Patron import Patron


zach = Patron()
braden = Patron()

zach.retrieve(1187277)

print zach.attributes
print braden.attributes

My console says that both the “zach” and “braden” instance have the exact same attributes, even though I have not set anything to the “braden” instance. If I assign something to the “braden” instance, then they both share the properties of that instance.

I think it’s a problem related to mutable default argument behavior, but I can’t figure out my problem.

  • 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-18T03:57:46+00:00Added an answer on June 18, 2026 at 3:57 am

    You have made attributes a class level variable and because dicts are mutable. Move it’s definition inside of __init__ and it should work.

    class demo(object):
        class_level = {'a': 0}
        class_level_nm = 0
        class_level2 = 0
        def __init__(self, v):
            self.instance_level = v 
            self.class_level['a'] += 1
            self.class_level_nm += 1
            demo.class_level2 += 1
        def __str__(self):
            return 'class level (mut): %d  class level (unmut): %d  instance level: %s  class level2: %d' % (self.class_level['a'],
                                                                                         self.class_level_nm,
                                                                                         self.instance_level,
        self.class_level2)
    
    a = demo('a')
    b = demo('b')
    
    print a
    print b
    
    c = demo('c')
    
    print a
    print b
    print c
    

    Gives:

    class level (mut): 2  class level (unmut): 1  instance level: a  class level2: 2
    class level (mut): 2  class level (unmut): 1  instance level: b  class level2: 2
    class level (mut): 3  class level (unmut): 1  instance level: a  class level2: 3
    class level (mut): 3  class level (unmut): 1  instance level: b  class level2: 3
    class level (mut): 3  class level (unmut): 1  instance level: c  class level2: 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class, called OneCell , here the definition: template <class T>
I have a template class, called Cell , here the definition: template <class T>
So I have this class here: package phil.droid.game; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle;
I have a class template in a c++ project: template<class RequestHandler = DefaultRequestHandler> class
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
I have a template class: template<class T> class A{ T a, b; public: A(A<T>
I have a class like template <class T> struct A{ template <class U> A(U
I have the following class: template <typename T> class matrix { private: int _n;
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a template class defined like so: template <class T> class Command {

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.