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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.