> I don't think so as this new __repr__ seems to be pretty similar to
> the __reduce__ function, which returns the proper information needed
> to build an identical copy: The rebuild function (almost always the
> constructor) and the arguments to this function.
> For classes with __reduce__, __repr could be implemented like the
> following snippet.
>
> def __repr__(self):
>    klass, args = self.__reduce__()
>    return '%s(%s) % (type.__name__, ', '.join(map(str,args)))
>
> The only problem would be with types where the rebuild function isn't
> the constructor, but AFAIR we don't have this case in PySide.

In
 principle there should be cases where this happens: QColor has 16-bit 
precision, which can only be recovered with QColor.fromRgbF. If 
you really want exact reconstruction you also need QColor.fromHsvF and 
QColor.fromHslF, as the data is not necessarily stored in RGB format.

I assume this would be considered as a bug in __reduce__?

The
 other problem with simply using __reduce__ is that it's not necessarily
 human-readable:

1. For __repr__ we may want to try hard to shorten 
the representation (using default arguments), whereas it might not be 
worth it to slow down __reduce__ for this reason.

2. You need 
specific context information for properly displaying enums, and 
__reduce__ doesn't give that:

  QPen()
= QPen(0, QBrush(QColor.fromRgbF(0, 0, 0, 1), Qt.SolidPattern), Qt.SolidLine, 
16,
  64, [], 0, 2)

wouldn't
 be possible using __reduce__.

3. There is also the issue of 
floating-point precision: QColor.fromRgbF only needs 6 digits, it would 
be overkill to display 12 digits. And there is also the annoying case
 where the floating-point precision is theoretically necessary, but not 
useful in practice (for example, it's not very useful to know that your 
brush is 0.90000000000000002 pixels wide), and you may want full precision in 
__repr__ and reasonable precision in __str__.


So for
 some types it would be useful to have some more metadata, but I agree 
that for many types __reduce__ should give all that's needed.

By the way, you'd want to map(repr,args), not map(str...).


Cheers,
Farsmo
                                          
_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to