is this a python bug?

Tim Peters tim.one at home.com
Wed Jun 6 22:15:47 EDT 2001


[Wolfgang Lipp]
> BTW, Mike, I tried your code,
>
> class Child:
>     def __init__(self, parent):
>         self.__dict__['parent'] = parent
>     def __getattr__(self, attr):
>         self.parent.a = 1
>         self.parent.b = 1
>         self.parent.c = 1
>         return getattr(self.parent, attr)
> class Parent:
>     def __init__(self):
>         self.a = Child(self)
> print Parent().__dict__
>
> and it *is* exactly the problem of mutation-while-accessing
> problem! thanks!

Michael Hudson brought this up on Python-Dev, and as a result this code now
prints (under current CVS Python):

{'a': <__main__.Child instance at 0076ABEC>, 'c': 1, 'b': 1}

So it doesn't blow up, but my guess is better than yours about what it will
actually print <wink>.  That is, the effect of mutating a container while
it's being traversed for printing is most likely never going to be clearly
defined.  The same goes for comparison functions that mutate a dict during,
e.g., dict key lookup.  We just added some butt-ugly code to stop it from
crashing.

the-good-news-is-that-sane-code-retains-sane-behavior-ly y'rs  - tim





More information about the Python-list mailing list