A question I have...

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 30 02:13:13 EDT 2010


On Sat, 30 Oct 2010 15:05:37 +1300, Lawrence D'Oliveiro wrote:

> In message <mailman.168.1287872943.2218.python-list at python.org>, geremy
> condra wrote:
> 
>> ... dividing strings by a number doesn't make sense.
> 
> The logical meaning would be the opposite of multiplying strings by a
> number:
>
>     >>> "abc" * 3
>     'abcabcabc'
>     >>> "abcabcabc" // 3
>     'abc'


That is a hyper-generalisation. Just because * and + are defined for 
strings doesn't mean we have to define other operators as well.

In general, given a string S, there is no substring s such that s*n = S 
and therefore in general S//n will almost always raise an error, for 
nearly all S and nearly all n. Of the infinite number of combinations of 
string S and integer n, only a vanishingly small proportion will define 
S//n. Given how rarely string division would apply, and the lack of any 
real-world use-case, there is no sensible reason to define S//n.

One could define functions that do anything, but very few of them are 
useful. Defining string//int, or string//string, are not some of the 
useful ones. One might as well define math.sqrt("spam\nspam\nspam\spam") 
= "spam" (it's spam in a square, geddit?) and declare that's the 
"logical" meaning. But that would be silly.



-- 
Steven



More information about the Python-list mailing list