1. 3
  1.  

  2. 2

    So the cool thing with PEP302 is that you could in theory just implement a random language in Python that compiles using the ast module. Then when you load your library you can hook into the import system and on-the-fly compile your custom language before giving it to Python.

    Imagine if you had a LISP with macro support! You could just generate a bunch of Python code and hook into it transparently.

    https://youtu.be/1vui-LupKJI?t=978

    :)

    1. 2

      I don’t know about python, but perl has a similar mechanism. Which led to things like: https://metacpan.org/dist/Lingua-Romana-Perligata/view/lib/Lingua/Romana/Perligata.pm

      1. 1

        Author: Damian Conway.

        Figures.

        To simplify the mind-numbingly complex rules of declension and conjugation that govern inflexions in Latin, Perligata treats all user-defined scalar and array variables as neuter nouns of the second declension – singular for scalars, plural for arrays.

        Hashes represent something of a difficulty in Perligata, as Latin lacks an obvious way of distinguishing these “plural” variables from arrays. The solution that has been adopted is to depart from the second declension and represent hashes as masculine plural nouns of the fourth declension.

      2. 1

        I know of one module that utilizes this. pcapnp loads up Cap’n’Proto schema files and creates appropriate Python classes. Pretty neat IMO.

        1. 1

          Neat! I’m curious to find other similar (ab)uses of Python’s import mechanism.

        2. 1

          You can add macros to Python directly with this mechanism.

          (Hy is very cool too.)

        3. 1

          One of the limitations of hooking into the Python import system is that the modifications you require can’t be used directly on the script file. Instead, the file with modifications has to be imported into the script file. An alternative is to hook into the Python codecs.