[Python-Dev] Re: PEP 638: Syntactic macros

2023-02-01 Thread Stephen J. Turnbull
Joshua Herman writes:

 > I think that this would be better as a library in my opinion. 

There's a third party package called MacroPy that provides macros,
although I haven't heard anything about it in a couple of years.

I seem to recall that it's a preprocessor that hooks into the import
system.

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/VRY55MMVRVZGHJRPPWWHKHLPPI2HLHZR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: PEP 638: Syntactic macros

2023-02-01 Thread cdp49
Unfortunately, it's no longer being maintained.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/UEPTBUIK67NLNE4JBNN6JNJON3BRGSUK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: PEP 638: Syntactic macros

2023-02-01 Thread cdp49
I think that's exactly the problem with a lack of Python macros. The full 
quote, of course, goes: "There should be one-- and preferably only one 
--*obvious* way to do it."

Often, there's a mathematical notation for something, and *this* is the only 
obvious way to write anything out. But this doesn't work if you force every 
package to adopt the same syntax. For example, if you'd like a package to work 
with probabilities, it's very reasonable to want to write `x ~ Normal(0, 1)` to 
say x follows a normal distribution. This is the only syntax I consider natural 
for this problem; but packages can't do that, since `~` already has a meaning 
outside of probability.

Not to mention, DSLs are forced to adopt all kinds of weird syntax when the 
behavior of base Python doesn't align with what the DSL needs to do. Obviously, 
the only way to write out a `for` loop should be to use the `for` keyword. This 
doesn't work in JAX. If you want to use a `for` loop in JAX, you have to use 
the `lax.fori_loop` function, or else `for` will end up being unrolled, because 
of various requirements of the JAX compiler. Having to use `lax.fori_loop` is, 
to put it mildly, *incredibly* unpythonic.

This really is the biggest reason I switched to Julia: Python math is 
unpythonic. I don't want to be forced to learn lots of weird little functions 
like `np.matmul(x1, x2)` when there's already one obvious syntax I'm very 
familiar with: `x1 *  x2`.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/6QM4GMH5FDX2H5OZHCE33EOJHCV3TI2R/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: PEP 638: Syntactic macros

2023-02-01 Thread Stephen J. Turnbull
cd...@cam.ac.uk writes:

 > I think that's exactly the problem with a lack of Python
 > macros. The full quote, of course, goes: "There should be one-- and
 > preferably only one --*obvious* way to do it."

You understand that the Zen is humorous?  Most of the Zen, if taken
universally and seriously, advocates the impossible.  And as a whole,
it's definitely impossible even in the limited scope it's intended to
apply to -- it's internally inconsistent.

 > Often, there's a mathematical notation for something, and *this* is
 > the only obvious way to write anything out.

But that's not the way Python looks at it, you see.  First, "a"
mathematical notation doesn't exclude multiple notations, and for most
mathematical concepts there are indeed multiple common notations (eg,
for multiplication, juxtaposition of the factors, *, ・, and × are all
in common use depending on the kind of multiplication).  I'm pretty
sure Tim Peters was well aware of such.  Frequently the most commonly
used expressions are really ugly (at least in my experience in
mathematical applications to game theory ;-).  Now, you can recover
your position from that issue by appealing to other more or less Zen
desiderata (readability counts, for example), but they're not quite as
strong arguments here.

The second objection is more serious: the Zen is intended to be
restricted to Python.  "There should be one-- and preferably only one
--obvious way to do it [*in Python*]."  Guido (and the other OG core
devs) wanted consistent and obvious ways to do it *in Python*.  The
consistent and obvious way to write "X ~ N(0,1)" (oops, not so obvious
after all!) in Python is "X = Normal(0,1)", where presumably the
Normal class provides facilities such as CDF and PDF as well as the
PRNG of random.normalvariate.

 > Not to mention, DSLs are forced to adopt all kinds of weird syntax

This is more or less intentional, though, as is the restriction to a
predefined set of operator symbols (you can't define ・ as an operator
symbol when you need two kinds of multiplication for example).  Python
has consistently refused to be turned into a platform for DSLs for
almost 3 decades.  Ruby and Lisps are better for that.

You don't have to like that (quite a few people don't, of course
that's why MacroPy was written), but it's really not un-Pythonic.
There is method to this madness: Python aims for readability and
flexibility for the community of Python programmers who might
encounter the code, rather than catering to authors and their domain
specialist community.  The fact that Python adoption is still growing
should tell you something about the preferences and needs of the
general Python community.

 > I don't want to be forced to learn lots of weird little functions
 > like `np.matmul(x1, x2)` when there's already one obvious syntax
 > I'm very familiar with: `x1 * x2`.

I don't recall ever writing matrix multiplication that way in
mathematics though.  That's universally written as juxtaposition in my
experience.  And the obvious way to write it in Python (and np) has
been "x1 @ x2" for some years now.  In np, "*" means element-wise
multiplication, I believe.

Perhaps some BDFL will arise to merge the benefits of Python with
those of Julia, but for the near term we're all going to have to
choose one or the other.

Steve

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/D2427HD63JN5MTBFQL2SZFBJU3UEXE3L/
Code of Conduct: http://python.org/psf/codeofconduct/