tags:

views:

77

answers:

2

What is your opinion of using the infix operator hack in production code? Issues:

  • The effect this will have on speed.
  • The potential for a clashes with an object with these operators already defined. This seems particularly dangerous with generic code that is intended to handle objects of any type.

It is a shame that this isn't built in - it really does improve readability

+1  A: 

In my personal opinion this would not be a great idea in production code: the biggest problem with it is that it totally non-standard and will probably leave non-familiar readers wondering where this novel syntax has suddenly sprung from.

I think you should prefer clarity rather than succinctness in general - Python is not C!

jkp
+4  A: 

It will be measurably slower than more Pythonic code, fragile (e.g. in the way you suggest), and baffling to every expert Python programmer that comes upon such code for the first time.

If you want to turn Python into one of the very few languages that allow user-defined infix operators (such as Haskell), you're better off designing a way to alter the syntax dynamically for the purpose, implement it as a patch to Python's parser, and start lobbying for it -- if it improves readability as much as you say, then it shouldn't be that hard to get Guido's approval for a clean, easy-to-explain implementation (if Guido, as I suspect, should sternly reject it instead, then you may want to ponder who's a better judge of language readability: you, or the designer of one of the most readable widespread languages? but I can't channel Guido, that's the *tim*bot's job;-).

Alex Martelli
1) Isn't there a Python language freeze atm?2) I think the Python syntax is running out of unused characters, so adding such a feature would almost certainly break code. There *might* have been a chance if the change was proposed before 3.x, but I don't think its likely at all at the moment.
Casebash
@casebash, (1) yes for now, but not forever, (2) `'?'` is still free, backquote is free in 3 (was taken in 2), and there's a _lot_ of Unicode punctuation available (since Python 3 goes by utf8) if we need more. So I don't think the chance is any less (or more) now than it was during Python 3's design phase.
Alex Martelli
Wouldn't unicode be difficult to type?
Casebash
@Casebash, depends on your system, keyboard and settings -- on a Mac, for many popular Unicode glyphs, it's → quite ♥ easy indeed, be it in ☂ rain or ☀ sunshine...☆★✮✯...
Alex Martelli