for convenience

Avi Gross avigross at verizon.net
Thu Mar 24 21:48:35 EDT 2022


Yes, Chris, you can do all kinds of useful things in Python and I can not make much of
a case for requiring a pre-processor. The main reason would be to make code
that interprets faster or produces a smaller file of Python commands.

All I was saying was that there might be a scenario where a textual replacement
method could happen the way Paul sort of guessed.

I note that some Python files can be partially processed once into bytecode and then
run many times. That may be a level where a preprocessor phase might customize it
better so what runs is already half-configured.

And as noted, anyone porting code from another language where the code is
already segmented with #IFDEF statements and other such fairly primitive
methods might have to work harder to move that too into their Python program.

I vaguely recall having to write such a preprocessor once upon a time as an exercise
for a programming class and it definitely would have been much easier to do in
Python. 


-----Original Message-----
From: Chris Angelico <rosuav at gmail.com>
To: python-list at python.org <python-list at python.org>
Sent: Thu, Mar 24, 2022 7:57 pm
Subject: Re: for convenience


On Fri, 25 Mar 2022 at 10:44, Avi Gross <avigross at verizon.net> wrote:
> But would it be helpful? Maybe. I am thinking back to decades ago when I
> did C and C++ programming and how we used it. It was way more that just:
>
> #DEFINE TIMEZONE 5
>
> The above use is sort of to create a constant. What we often used was ways
> to customize the code so different code would be compiled say when testing
> or to work one way on machine A with operating system A' versus machines
> B and C, perhaps with different needs and abilities. Sometimes all kinds
> of features only made it into one version of the other. The compiler saw
> different code each time.

Yes, I'm aware of how it was used... but to what extent is that
actually necessary in Python? When do you ever need something that you
can't do with simple conditional function definition or something of
the sort?

Textual manipulation lets you create all manner of nightmares. A lot
of them are acceptable evils because of the benefits gained, but you'd
be hard-pressed to pitch those same benefits to Python programmers,
where most of the differences are already handled by (eg) the
functions in the os module.

> I have also written many things that effectively are read by an early stage
> and selected regions are parsed and replaced with code in another language
> so that by the time a compiler or interpreter sees it, ...
>
> So sure, you could write code that reads fileA.py and outputs fileB.py and only
> run fileB through the interpreter. Yes, instead of making versions which have all
> kinds of text written in various languages, you could just have the python code
> read in various files at run time or other techniques.

And that's what I meant by having your own preprocessor. I've done
various DSLs that way, having something that effectively compiles to
Python code. (I've also used Python to write preprocessors for things
in other languages, since Python is excellent at text manipulation and
even has things like JavaScript lexers available on PyPI.)

But the best of these are NOT things that the C preprocessor would be
capable of. They can have arbitrarily complex levels of syntactic
comprehension, making them far safer to work with.


ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list