Python Worst Practices

Chris Angelico rosuav at gmail.com
Tue Mar 3 06:12:09 EST 2015


On Tue, Mar 3, 2015 at 7:51 PM, Christian Gollwitzer <auriocus at gmx.de> wrote:
> Am 28.02.15 um 02:44 schrieb Chris Angelico:
>> On Sat, Feb 28, 2015 at 12:32 PM,  <sohcahtoa82 at gmail.com> wrote:
>>> For example, I've seen someone create a Socket class, then created an operator overload that allowed you to "add" a string to your socket to make the socket send the string, with the result being a status code indicating success or an error.
>>>
>>
>> Why not left shift the socket by that string, the result being the
>> original socket? At least that has precedent...
>>
>
> 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.

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

ChrisA



More information about the Python-list mailing list