[Python-Dev] Guarantee ordered dict literals in v3.7?

Steven D'Aprano steve at pearwood.info
Wed Dec 20 18:50:03 EST 2017


On Wed, Dec 20, 2017 at 03:23:16PM -0800, Chris Barker wrote:
> On Wed, Dec 20, 2017 at 2:31 AM, Steven D'Aprano <steve at pearwood.info>
> wrote:

> >  Even when it works, the guarantee is quite weak. For instance, even
> > the object type is not preserved:
> >
> > py> class MyDict(dict):
> > ...     pass
> > ...
> > py> d = MyDict()
> > py> x = eval(repr(d))
> > py> assert d == x
> > py> assert type(d) == type(x)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > AssertionError
> >
> 
> Oh come on! If you subclass, and don't override __repr__ -- you're written
> a (very slightly) broken class (OK, a class with a broken __repr__).

Why is it broken? Is it documented somewhere that every subclass MUST 
override __repr__?

If there's a bug here, and I'm not sure that there is, the bug is in 
dict itself, for having a repr which isn't friendly to subclasses.

But in practice, why would I care? Obviously sometimes I do care, and 
for debugging it is often good to have a custom repr for subclasses, but 
it isn't mandatory or even always useful.

Especially since in practice, it isn't that common to round-trip repr 
though eval (apart from the REPL itself, of course).



-- 
Steve


More information about the Python-Dev mailing list