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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:04:18+00:00 2026-05-16T08:04:18+00:00

I have a Python class containing a list, to which I append() values. If

  • 0

I have a Python class containing a list, to which I append() values.

If I delete an object of this class then create a second object later on in the same script, the second object’s list is the same as the first’s was at the time of deletion.

For example:

class myObj:
    a = []
    b = False

o = myObj()
o.a.append("I'm still here!")
o.b = True

del o
import gc
gc.collect() # just to be safe
o = myObj()

print(o.a) # ["I'm still here!"]
print(o.b) # False

There is a way to empty the list:

while len(o.a):
    o.a.pop()

But that’s ridiculous, not to mention slow. Why is it still in memory after garbage collection, and more to the point why is it not overwritten when the class inits? All non-list member vars are handled correctly. append(), extend() and insert() all lead to the same result — is there another method I should be using?

Here’s my full test script. Notice how Python gives an AttributeError if I try to delete a member list directly, even though I can read from it fine.

  • 1 1 Answer
  • 1 View
  • 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-16T08:04:19+00:00Added an answer on May 16, 2026 at 8:04 am

    When you create a variable inside a class declaration, it’s a class attribute, not an instance attribute. To create instance attributes you have to do self.a inside a method, e.g. __init__.

    Change it to:

    class myObj:
        def __init__(self):
            self.a = []
            self.b = False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python class which reads a config file using ConfigParser: Config file:
You have a Python class which needs an equals test. Python should use duck-typing
I have a Python file with as content: import re import urllib class A(object):
I have a small Python OOP program in which 2 class, Flan and Outil
I have a JavaScript string containing HTML like this: <div> <div class=a> content1 </div>
I have a python class that looks like this: class Process: def __init__(self, PID,
I would like to create instances of a class containing a list that's empty
I have a Python class called ClassA and another Python class which is supposed
If I have the following Python class: class Test(object): funcs = { me :
I have been working on a recursively defined list class in Python and I'm

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.