On 09/10/2013 16:26, Giovanni Porcari wrote:
Buongiorno a tutti

[...]
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)



[...]
     test(TestClassNewStyle)

[...]

--------- 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.


http://docs.python.org/2.7/reference/datamodel.html#special-method-lookup-for-new-style-classes


Ciao  Manlio
_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a