Buongiorno a tutti Dato il seguente codice :
------------------------------------------------ class TestClassOldStyle(): def __init__(self): self.store = dict() def __getattr__(self,name): print '__getattr__ :' ,name if not hasattr(dict,name): raise AttributeError("dict has no attribute '%s'" % name) return getattr(self.store,name) class TestClassNewStyle(object): def __init__(self): self.store = dict() def __getattr__(self,name): print '__getattr__ :' ,name if not hasattr(dict,name): raise AttributeError("dict has no attribute '%s'" % name) return getattr(self.store,name) def test(factory): x = factory() x['foo'] = 21 print x['foo'] print len(x) if __name__=="__main__": print ' --------- TestClassOldStyle ------' test(TestClassOldStyle) print '\n\n\n --------- TestClassNewStyle ------' test(TestClassNewStyle) Risultato : --------- TestClassOldStyle ------ __getattr__ : __setitem__ __getattr__ : __getitem__ 21 __getattr__ : __len__ 1 --------- TestClassNewStyle ------ Traceback (most recent call last): File "/Users/fporcari/Downloads/pyro_test/testobject.py", line 38, in <module> test(TestClassNewStyle) File "/Users/fporcari/Downloads/pyro_test/testobject.py", line 27, in test x['foo'] = 21 TypeError: 'TestClassNewStyle' object does not support item assignment ------------------------------------------------------------------------ In pratica vorrei che per ogni attributo che è definito in dict mi venisse restituita la chiamata su self.store mentre ogni attributo non definito in dict dovrebbe generare un attribute error. Magari è una cosa banale ma ora come ora non vedo perchè con le new style class non funzioni e non funzioni al punto di non mettere nemmeno le print di debug... Grazie :) G _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python