On 12/08/2013 07:14 AM, Amit Saha wrote:
>If all of the bitN are strings:
>         bit1 + bit2 + bit3 + bit4 + bit5
>actually constructs:
>         bit1+bit2
>         bit1+bit2+bit3
>         bit1+bit2+bit3+bit4
>         bit1+bit2+bit3+bit4+bit5
>A number of unneeded string object, and a very big useless memory weight.
Thanks Denis, good to know this is how it is done. I hadn't thought
about it simply because, never have probably concatenated strings
beyond the simple "adding a new line" to a string.

This is only due to catenation being defined in python as a _binary_ operation. In principle, there is no reason for this. In a hypothetical language (I will here use Lisp-like syntax just to make a visual difference), one may write:
    (cat bit1 bit2 bit3 bit4 bit5)
Then cat would catenate all bits in one go, into a sigle total string (sum the sizes and make a single whole string of this size).

As a side-note, this is also true for common arthmetic operators like + or *, which are not conceptually binary, but we have this impression *due to infix notation*:
    1 + 2 + 3 + 4 + 5
is usually interpreted as a series of binary operations:
    ((((1 + 2) + 3) + 4) + 5)
but there could well be (and in some languages this is the case):
    (+ 1 2 3 4 5)
in one go (even if behind the stage there are binary ops, just because the machine also knows that).

Think at manual sum for illustration:
   123
   456
   789
   ---
result

Maybe infix notation is just wrong for some operations and misleads into wrong thinking. We should reserve for actual binary ops, namely - and /; but prefix notation, as illustrated above, would nicely do the job for binary pos as well. (The same point applies to logical operators or & and, which are not binary, while the case of relational ones [comparisons] is more obscure.) However, unlike the case of catenation, unjustified binary arithmetic operations do not cause any other harm than needlessly multiplying function or method lookups & calls (if there is a call, which is the case in python because such operators can be overloaded).

Denis
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to