Rewriting __getattr__

kost BebiX k.bx at ya.ru
Fri Jan 7 16:54:24 EST 2011


07.01.2011, 17:47, "Steven D'Aprano" <steve+comp.lang.python at pearwood.info>:
> On Fri, 07 Jan 2011 16:47:55 +0200, kost BebiX wrote:
>
>>  Well, actually the code you showed doesn't work)
>
> Actually, it does. It just prints a warning message as well. Look
> carefully:
>
>>>>>  class A(object):
>>  ..     def __init__(self):
>>  ..         self.d = {}
>>  ..     def __getattr__(self, key):
>>  ..         try:
>>  ..             return self.d[key]
>>  ..         except KeyError:
>>  ..             raise AttributeError
>>>>>  from copy import deepcopy
>>>>>  a = A()
>>>>>  deepcopy(a)
>>  Exception RuntimeError: 'maximum recursion depth exceeded while calling
>>  a Python object' in <type 'exceptions.AttributeError'> ignored
>>  Exception RuntimeError: 'maximum recursion depth exceeded while calling
>>  a Python object' in <type 'exceptions.AttributeError'> ignored
>>  0: <__main__.A object at 0xda0250>
>
> The last thing printed is the deepcopied object.
>
> I've tested the above code in Python versions 2.4 through 3.2 and the
> only one that prints that message is 2.6.
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list

So maybe it should be fixed in 2.6? When I'll have time I'll look at copy.py in different python's. Maybe there's the answer)
Thanks anyway.



More information about the Python-list mailing list