I create a class with a dictionary variable, and a method for editing it:
class Test:
TestDict = {"TestKey": "OldValue"}
def ChangeDictionary(self):
self.TestDict["TestKey"] = "NewValue"
Then, i create a two objects:
ObjectOne = Test()
ObjectTwo = Test()
Print their dictionaries before ChangeDictionary method call, and after:
# Print values before method call
print(ObjectOne.TestDict)
print(ObjectTwo.TestDict)
# Call method
ObjectTwo.ChangeDictionary()
# Print values after method call
print(ObjectOne.TestDict)
print(ObjectTwo.TestDict)
And i get this output:
{'TestKey': 'OldValue'}
{'TestKey': 'OldValue'}
* * Method call * *
{'TestKey': 'NewValue'}
{'TestKey': 'NewValue'}
So method ChangeDictionary which was called from second object changed value also and in first object.
Is it Python language bug or reasons of it somewhere else?
Updated: If class variable will be any other type (For example - String) - In first object variable will have old value:
class Test:
TestString = "OldValue"
def ChangeString(self):
self.TestString = "OldValue"