Python Worst Practices

Christian Gollwitzer auriocus at gmx.de
Tue Mar 3 17:46:47 EST 2015


Am 03.03.15 um 12:12 schrieb Chris Angelico:
> On Tue, Mar 3, 2015 at 7:51 PM, Christian Gollwitzer <auriocus at gmx.de> wrote:
>
>> Are you trying to pick on C++ streams? I could never understand why
>> anybody has problems with an arrow << that means "put into the left
>> thing" instead of "shift the bits to the left". How often do you use
>> bitshift operations in your programs as opposed to output? Ot would be
>> equally silly to complain, that in Python you divide a string by a
>> tuple, and the modulus gives you a formatted string.
> 
> I am, yes. Both your examples seem lovely and simple when you first
> look at them, but operator precedence means you get weird edge cases.
> In the case of string modulo, there's another edge case as a
> consequence of the operator being, by necessity, binary. A function
> call makes better sense here.

I can agree with the argument that operator precedence can make
problems; e.g. this

	cout<<a==b;

does not output the truth value of a==b, but instead outputs a and
compares the stream to b (which will usually fail to compile, but still).

But the argument that << is a left-shift and nothing else is silly. <<
for bitshift is nothing more intuitive than % for modulus (where in math
does this symbol occur?) or [] for indexing. We just got used to it, and
to me << as an arrow for putting someting into a stream seems pretty
obvious.

> Operator overloading in each case here is "cute", not optimally practical.

Maybe just sub-optimal? With today's C++ one could use a variadic
template and still have type-safe compile-time bound output formatting.
This hasn't been possible in the original iostream library back then.

	Christian



More information about the Python-list mailing list