On Mon, Nov 14, 2016 at 1:21 PM, Mikhail V <mikhail...@gmail.com> wrote:

> On 14 November 2016 at 12:16, Steven D'Aprano <st...@pearwood.info> wrote:
> > On Mon, Nov 14, 2016 at 01:19:30AM +0100, Mikhail V wrote:
> >
> > [...]
> >> >> A good syntax example:
> >> >>
> >> >> a = sum (a, c)
> >
> > There is a reason why mathematicians and accountants use symbols as a
> > compact notation for common functions, instead of purely functional
> > notation.
> >
> > Here is a real-world example, the formula for compound interest:
> >
> > A = P*(1+r/100)**n
> >
> > Compare that to:
> >
> > A = mul(P, pow(sum(1, div(r, 100)), n))
> >
> > Even Reverse Polish Notation is easier to read than that series of
> > functions:
> >
> > P 1 r 100 / + n ** *
> >
> >
> > Don't misunderstand me, functions are important, and over-use of cryptic
> > symbols that are only meaningful to an expert will hurt readability and
> > maintainability of code. But people have needed to do arithmetic for
> > over six thousand years, and there is no good substitute for compact
> > operators for basic operations.
>
> I agree. Actually I meant that both are good examples.
> In some cases I still find function-style better, so this:
>
> A = P*(1+r/100)**n
>
> I would tend to write it like:
>
> A = P * pow((1 + r/100) , n)
>
> For me it is slightly more readable, since ** already makes the
> equation inconsistent.
>
>
Okay, then make a proposal to get the operators included as built-ins.
Once you have gotten that approved, then we can start talking about which
syntax is better.  But the idea that people should import a module to do
basic mathematical operations is a non-starter.


> Most of problems with function-style equations come from limitations
> of representation
> so for Courier font for examples the brackets are too small and makes
> it hard to read.
> With good font and rendering there no such problems. Some equation editors
> allow even different sized brackets - the outer extend more and more
> when I add nested equations, so it looks way better.
>
>
You shouldn't be using a variable-width font for coding anyway.  That is
going to cause all sorts of problems (indentation not matching up, for
example).  You should use a fixed-width font.

But if brackets are a problem, I don't see how using more brackets is a
solution (which is the case with function calls).  On the contrary, I would
think you would want to minimize the number of brackets.


>
> > [...]
> >> It is kind of clear from the context, that I am speaking of syntax and
> >> not how things are working under the hood, or?
> >> If a compiler cannot optimize "a = a + 1" into an in-place operation,
> >> that is misfortune.
> >
> > That's not how Python works. Or at least, not without an extremely
> > powerful and smart compiler, like PyPy.
> >
> > In Python, integers (and floats) are immutable objects. They have to be
> > immutable, otherwise you would have things like this:
> >
> > x = 1
> > y = x
> > x = x + 1  # changes the object 1 in place
> > print(y*10)  # expect 10, but get 20
> >
> > That's how lists work, because they are mutable:
> >
> > py> x = [1]
> > py> y = x
> > py> x[0] = x[0] + 1
> > py> print(y[0]*10)  # expect 1*10 = 10
> > 20
> >
> >
> > A "sufficiently smart" compiler can work around this, as PyPy does under
> > some circumstances, but you shouldn't expect this optimization to be
> > simple.
> >
> > [...]
> >> A better option would be to support unary operators so the user
> >> can write directly without assignment:
> >>
> >> inc (a, 1)
> >>
> >> Would mean in-place increment "a" with 1
> >
> > That is impossible with Python's executation model, in particular the
> > "pass by object sharing" calling convention. I expect that this would
> > require some extremely big changes to the way the compiler works,
> > possibly a complete re-design, in order to allow pass by reference
> > semantics.
>
> Thanks a lot for a great explanation!
> So for current Python behavior, writing
>
> x = x + 1
>
> and
>
> x += 1
>
> Would mean the same in runtime? Namely creates a copy and assigns
> the value  x+1  to x. Or there is still some overhead at parsing stage?
> Then however I see even less sense in using such a shortcut,
> it would be good to dismiss this notation, since it makes hard
> to read the code.
>

No, that is only the case for immutable types like floats.  For mutable
types like lists then

x = x + y

and

x += y

Are not the same thing.  The first makes a new object, while the second
does an in-place operation.  Sometimes you want a new object, sometimes you
don't.  Having both versions allows you to control that.


> As for Numpy, it uses anyway its own approaches, so
> in-place should use own syntax, e.g. like numpy.sum(x_, a) to
> do it in-place.
>
>
First, there is no "_" suffix for variables that numpy could use.  The
syntax you are suggesting isn't possible without a major change to the
basic python grammar and interpreter.

Second, again, "sum(x, a)" does not add "x" and "a".

Third, again, why?  You still haven't given any reason we should prefer
this syntax. If this is just your personal preference, please say that.
But if you expect people to add a bunch of new built-ins, implement your
new suffix syntax, and implement your massive changes to how python
operations and function calls work, then you are going to need something
more than personal preference.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to