I am building a python class in which I create a dictionnary using the method "new". Then, I would like to define a method to affect a unit for some of the keys. Below an example of the code:
class MyClass(object):
def __new__(cls):
dict = {key1:None, key2:None}
return dict
def unit(self, unit):
self[KEY].unit = unit
Where, in the unit method, KEY should take any key of the dictionnary. Could you please help me to understand how to code it?
In advance thank you,
__new__method. It's almost always a mistake and is definitely so in your example. You should be subclassingdict. It is also not clear where or how the magicKEYvar is defined. I recommend you rephrase your question in terms of your goals rather than in terms of getting your example to work using the__new__()method.