This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Class variable is still accessible after class instance has been overwritten out
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Nadas, eric.smith, john745, moird
Priority: normal Keywords:

Created on 2019-07-18 21:55 by moird, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
variabletest.py moird, 2019-07-18 21:55
Messages (5)
msg348130 - (view) Author: Daniel (moird) Date: 2019-07-18 21:55
Not sure if this is the desired behavior but wanted to bring it up anyways.
When you have a class and have a variable in like:
class TestClass(object):
    variable = []

then you run it through a loop like:
for num in range(10):
    test = TestClass()
    test.variable.append(num)

even if you assign another variable to it or none like:
test = "blah"
test = None

then reassign the class:
test = TestClass()
print(test.variable)

will return all the numbers in the list.
also doesn't seem to matter if garbage collection was manually ran.

Attached is a small example code.
This was found on Macos and tested with both python 3.7 and 2.7 Subsequently same on ubuntu with python 3.5 and python 2.7
msg348131 - (view) Author: Dominik Miedziński (miedzinski) * Date: 2019-07-18 22:32
This is expected, because list is created during class definition, not initialization. You should initialize `variable` in __init__.

class TestClass(object):
    def __init__(self):
        self.variable = []

for num in range(10):
    test = TestClass()
    test.variable.append(num)
msg348132 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-07-19 01:21
Because variable belongs to the class, and not any instance of the class, you can duplicate this behavior without creating any instances at all:

>>> class TestClass(object):
...     variable = []
...
>>> TestClass.variable.append(1)
>>> TestClass.variable
[1]

And then when you create an instance t, t.variable still refers to the class:

>>> t = TestClass()
>>> t.variable
[1]
>>>

This is expected behavior.
msg348136 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-07-19 01:29
Also, you might want to search for "python class and instance variables". This one looks decent, although I didn't read it exhaustively: https://howchoo.com/g/nzy0mthhyzl/understanding-class-vs-instance-variables-in-python-3
msg348178 - (view) Author: Daniel (moird) Date: 2019-07-19 14:05
Thanks, just didn't expect that behavior.
History
Date User Action Args
2022-04-11 14:59:18adminsetgithub: 81806
2020-08-06 11:54:27eric.smithsetmessages: - msg374930
2020-08-06 11:51:32john745setnosy: + john745
messages: + msg374930
2020-01-27 14:57:17xtreaksetmessages: - msg360765
2020-01-27 14:45:08miedzinskisetnosy: - miedzinski
2020-01-27 14:27:02Nadassetmessages: + msg360765
2020-01-27 02:56:17xtreaksetmessages: - msg360737
2020-01-26 23:51:39Nadassetnosy: + Nadas
messages: + msg360737
2019-07-19 14:05:54moirdsetmessages: + msg348178
2019-07-19 01:29:34eric.smithsetmessages: + msg348136
2019-07-19 01:21:01eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg348132

resolution: not a bug
stage: resolved
2019-07-18 22:32:13miedzinskisetnosy: + miedzinski
messages: + msg348131
2019-07-18 21:55:14moirdcreate