[PyQt] Repr for "value" objects

Giovanni Bajo rasky at develer.com
Mon Dec 17 15:59:51 GMT 2007


On 12/17/2007 4:25 PM, Andreas Pakulat wrote:

>>> Hi,
>>>
>>> there are many classes in PyQt which would benefit from a custom
>>> __repr__ method to show their contents. For instance, I'm getting tired
>>> of having to write:
>>>
>>>     def mousePressEvent(self, e):
>>>         print e.pos().x(), e.pos().y()
>>>
>>> instead of:
>>>
>>>     def mousePressEvent(self, e):
>>>         print e.pos()
>>>
>>> just because I otherwise get "<PyQt4.QtCore.QPoint object at 0x298EA468>".
>>>
>>> Are there any plans to add this to PyQt?
>> It's on the TODO list.
>>
>>> I'm happy to provide a list of 
>>> objects that might have a custom repr method, and even to actually write
>>> the code (if Phil explains me how he prefers this to be submitted --
>>> given metasip).
>> A list would help. The code would help more (just a patch against the source 
>> would be fine).
> 
> Wouldn't it be possible to utilize the already-existing operator<< for
> QTextStream for this? I mean Qt itself can already "pretty print" almost
> all their basic datatypes, so it seems like a good idea to use that code
> to produce some string for __repr__. 
> 
> BTW: Does PyQt allow to use qDebug() as in C++? (qDebug() << foo <<
> "somestring") 

Not really: QPoint can be formatted only to a QDataStream. QTextStream 
is limited to basic datatypes like float, char, etc.

Moreover, I would still prefer a Python-standard syntax for a 
Python-standard method like __repr__; the standard syntax for the string 
returned by __repr__ (for objects with a meaningful __eq__) is a Python 
expression which can be evaluated.
-- 
Giovanni Bajo



More information about the PyQt mailing list