[Python-Dev] __str__ vs. __unicode__

M.-A. Lemburg mal at egenix.com
Wed Jan 19 12:42:15 CET 2005


Walter Dörwald wrote:
> M.-A. Lemburg wrote:
>> So the question is whether conversion of a Unicode sub-type
>> to a true Unicode object should honor __unicode__ or not.
>>
>> The same question can be asked for many other types, e.g.
>> floats (and __float__), integers (and __int__), etc.
>>
>>  >>> class float2(float):
>> ...     def __float__(self):
>> ...             return 3.141
>> ...
>>  >>> float(float2(1.23))
>> 1.23
>>  >>> class int2(int):
>> ...     def __int__(self):
>> ...             return 42
>> ...
>>  >>> int(int2(123))
>> 123
>>
>> I think we need general consensus on what the strategy
>> should be: honor these special hooks in conversions
>> to base types or not ?
> 
> 
> I'd say, these hooks should be honored, because it gives
> us more possibilities: If you want the original value,
> simply don't implement the hook.
> 
>> Maybe the string case is the real problem ... :-)
> 
> 
> At least it seems that the string case is the exception.

Indeed.

> So if we fix __str__ this would be a bugfix for 2.4.1.
> If we fix the rest, this would be a new feature for 2.5.

I have a feeling that we're better off with the bug fix than
the new feature.

__str__ and __unicode__ as well as the other hooks were
specifically added for the type constructors to use.
However, these were added at a time where sub-classing
of types was not possible, so it's time now to reconsider
whether this functionality should be extended to sub-classes
as well.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 10 2005)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the Python-Dev mailing list