Why tuple with one item is no tuple

Daniel Dittmar daniel at dittmar.net
Wed Mar 16 18:00:51 EST 2005


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.

Operator overloading is certainly an irregular verb:
- I make the usage more intuitive
- Yours may lead to misinterpretation
- He obfuscates

Daniel



More information about the Python-list mailing list