0

Hi, I have got a little problem with __setitem__ method. It works fine in single class, but if class member is instance of the same class method __setitem__ doesn't work on class member but on class. Here is an example:

Here is a class with __setitem__ method:

class MyClass:
	attr = {}
	
	def __setitem__(self, key, value):
		self.attr[key] = value

and here i work with the class

>>> obj = MyClass()
>>> obj['key1'] = "value1"
>>> obj.attr
{'key1': 'value1'}
>>> obj.newinstance = MyClass()
>>> obj.newinstance.attr
{'key1': 'value1'}
>>> obj.newinstance['key1'] = "value2"
>>> obj.attr
{'key1': 'value2'}
>>>

as you see __setitem__ on newinstance works with self.attr from parent class. How can I hack this?

1
Contributor
1
Reply
4
Views
8 Years
Discussion Span
Last Post by vl4kn0
0

The answer is pretty lame :D The var __attr should be in __init__ method that's all

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.