Why tuple with one item is no tuple

Antoon Pardon apardon at forel.vub.ac.be
Fri Mar 18 05:17:35 EST 2005


Op 2005-03-16, Daniel Dittmar schreef <daniel at dittmar.net>:
> Diez B. Roggisch wrote:
>> I reread his example and have to admit I'm confused: He complains about
>> having written his _own_ vector class - and concatenation and addition had
>> to use both + ?
>
> I've interpreted it as:
> If Python had choosen different operators for addition and sequence 
> concatenation, I could have implemented them both in my vector class. As 
> it is, I have to implement one of them using a non-standard operator.
>
>> The examples focus too much on numbers - if we use instead 
>> 
>> ("foo")
>> 
>> we would get a iterable yielding ["foo",] or - as string already supports
>> iteration - ['f', 'o', 'o']. Which one to chose?
>
> What I was hinting at (NOT proposing, I'd hate this) was that integers 
> implement the [] operator. 5 [0] would then return 5, for all practical 
> purposes, it would look like a tuple. String already implements []. Yes, 
> that would lead to really surprising and inconsistent behaviour.
>
>>>I find this 'creative use of overloading' rather awful. But what the
>>>heck, I find list comprehension rather awful.
>> 
>> 
>> Well, the number of operators built into the language is limited - and I
>> actually prefer to have the possibility to overload these if I want to.
>> Nobody forces me - I could use
>> 
>> v1.concat(v2)
>> 
>> for two vectors v1, v2 if I wanted to.
>
> My peeve is about having operators added to standard types. This 
> increases the chances that using an object the wrong way leads to a 
> bogus result, not a runtime error. A more common programming error I 
> commit is passing a string where a list ist expected. And then I wonder 
> why later operations work on one-character strings.

The standard answer to this seems to be to use unittesting.

-- 
Antoon Pardon



More information about the Python-list mailing list