[PyKDE] Printing empty QStrings

Johannes Sixt Johannes.Sixt at telecom.at
Sat Nov 4 22:31:15 GMT 2000


On Wed, 01 Nov 2000, Phil Thompson wrote:
>Johannes Sixt wrote:
>> 
>> On Wed, 01 Nov 2000, you wrote:
>> >Boudewijn Rempt wrote:
>> >> >>> print (QString())
>> >> None
>> >> >>> print (QString(""))
>> >>
>> >> >>> print (QString("a"))
>> >> a
>> >>
>> >> instead of:
>> >>
>> >> >>> print (QString())
>> >>
>> >> Traceback (most recent call last):
>> >>   File "<stdin>", line 1, in ?
>> >>   File "/usr/local/lib/python2.0/site-packages/qt.py", line 898, in
>> >> __str__
>> >>     return str(self.sipThis)
>> >> TypeError: __str__ returned non-string (type None)
>> >
>> >This is sensible, but can't be achieved. It is Python raising the
>> >exception (not PyQt) because PyQt is returning the None object instead
>> >of a string object. PyQt could return a string object containing "None"
>> >(although "null" would be better) but then 'print QString()' and 'print
>> >QString("None")' would do exactly the same thing.
>> 
>> IMHO, it is an error to distinguish between an "empty" string and a "null"
>> string. In a program you usually want that they behave identical
>> (concatenation, searching, replacing, etc...), they should even compare equal
>> (if you have 2 string variables, a and b, one contains the empty string, the
>> other contains the null string, then you want that a==b is true in almost any
>> case!).
>> 
>> So, why make a difference at all?
>> <algebra>
>> My suggestion: treat them both like the neutral element of the monoid of
>> strings and the concatenation operation, i.e. the so-called emtpy string.
>> </algebra>
>
>What does Qt do? What gives the fewest surprises to a C++ Qt programmer
>coming to PyQt?

For every operation (concatenation, copying, searching & replacing, etc...), Qt
treats the null QString like an empty QString (but I must admit that I'm not
absolutely sure). The docs say (see QString::isNull()): "A null string is also
an empty string", but they also say (see QString::isEmpty()): "An empty string
is not always a null string".

I just checked the docs (Qt 2.2.1) again, and here is the big bulls**t:

    bool operator== (const QString & s1, const QString & s2)
    [...] A null string is different from an empty, non-null string.

Surprise! I'd say, that is the most b****d***ged thing the Trolls have invented!

The other difference they make is that one can test for the null string. IMHO,
this is also broken design and completely unnecessary. In my programs I don't
make any use of this "feature".

For me, the least surprising thing would be if there were no difference - IOW,
if there were no such thing called a "null string".

-- Hannes




More information about the PyQt mailing list