On Sun, 15 Nov 2015 02:43 am, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 10:40 PM, Steven D'Aprano <st...@pearwood.info> > wrote: >> Python has operator overloading, so it can be anything you want it to be. >> E.g. you might have a DSL where +feature turns something on and -feature >> turns it off. > > By that argument we should also have operators ~, !, $, \, ? because > some hypothetical DSL might someday want to use them for something.
No, I'm not saying that. I'm saying that since Python *already* has unary plus, it can be overloaded to do anything you want. I'm not saying that hypothetical DSLs are a good reason to add a bunch of arbitrary new do-nothing-by-default operators. (By the way, Python already has the ~ bitwise not operator.) >> Decimal uses it to force the current precision and rounding, regardless >> of what the number was initiated to: >> >> Counter uses it to strip zero and negative counts: >> >> I would expect that symbolic maths software like Sympy probably has use >> of a unary plus operator, but I'm not sure. > > Unary plus as normalization does not strike me as being very > intuitive. Well, in truth the only truly intuitive interface is the nipple, but using + for normalisation works for me. It's no more weird than ** for raising to the power, % for remainder after division, or != for not equal. >> I might consider stealing an idea from Perl and Javascript, and have >> unary plus convert strings to a number: >> >> +"123" >> => returns int 123 >> +"1.23" >> => returns float 1.23 > > Eww. Well of course it's "eww" when you write it as a literal, but you wouldn't do that except to illustrate the concept, that's just silly. But compare: number = +raw_input("enter a number: ") versus: text = raw_input("enter a number: ") try: number = float(text) except ValueError: number = int(text) Or worse, people who don't think of try...except will probably try parsing the string by hand. text = raw_input("enter a number: ").strip() if text.isdigit() or text.startswith("-") and text[1:].isdigit(): number = int(text) else: # Floats are allowed a leading - or +, no more than one decimal point, # no more than one exponent starting with E or e, and an optional # leading + or - sign in the exponent. Everything else must be digits. mantissa, exponent = number.upper().split("E", 1) ... And that's about the point where I stopped because correctly parsing floats by hand is complicated. But the point is that there was a time in my life as a Python programmer where I absolutely would have done that, rather than thinking of try...except. -- Steven -- https://mail.python.org/mailman/listinfo/python-list