14

I use to define macros (not just constants) in C like

#define loop(i,a,b) for(i=a; i<b; ++i)
#define long_f(a,b,c) (a*0.123 + a*b*5.6 - 0.235*c + 7.23*c - 5*a*a + 1.5)

Is there a way of doing this in python using a preprocess instead of a function?

*By preprocess I mean something that replaces the occurrences of the definition before running the code (actually not the whole code but the rest of the code, because since it's part of the code, I guess it will replace everything during runtime).

If there is, worth it? Will there be a significant difference in run time?

Carlos Pinzón
  • 1,286
  • 2
  • 15
  • 24
  • 2
    Traditionally the C preprocessor was a separate program, and it's still available as `cpp` in most POSIX-like systems. There are of course many other preprocessors available, [M4](https://en.wikipedia.org/wiki/M4_%28computer_language%29) being the most notable these days. There are also other C-like preprocessors if you just search a little. *However*, most Python programmers would frown at you and your code if you used a preprocessor and macros like that. Most C programmers as well actually. – Some programmer dude Apr 08 '16 at 04:51
  • 1
    Python is an interpreted language, there isn't a concept of a C-like pre-processor that replaces tokens before compilation. – Jason De Arte Apr 08 '16 at 04:54
  • 1
    Have a look at https://github.com/trentm/preprocess . – asmaier Jan 24 '18 at 17:13

3 Answers3

10

Is there a way? Yes. There's always a way. Should you do it? Probably not.

Just define a function that does what you want. If you are just concerned about code getting really long and want a one-liner, you can use a lambda function.

long_f = lambda a,b,c: a*0.123 + a*b*5.6 - 0.235*c + 7.23*c - 5*a*a + 1.5
long_f(1,2,3) == 28.808

And of course your first example is already way prettier in Python.

for i in range(a,b):
    ...

Edit: for completeness, I should answer the question as asked. If you ABSOLUTELY MUST preproccess your Python code, you can use any programming language designed for templating things like web pages. For example, I've heard of PHP being used for preprocessing code. Instead of HTML, you write your code. When you want something preprocessesed, you do your PHP blocks.

dda
  • 6,030
  • 2
  • 25
  • 34
phsyron
  • 520
  • 3
  • 17
8

Well, if you're going to perform some really hard calculations that could be performed in advance, then, perhaps, this makes sense: usually users are more happy with fast programs rather than slow ones.

But, I'm afraid python isn't a good choice when it comes to 'raw performance', that is, speed of arithmetic calculations. At least if we talk about the standard python implementation, called CPython.

Alternatively, you could check other variants:

  • PyPy. This is an alternative python implementation, in pure Python. Thanks to a JIT compiler it gives better performance but requires a lot more memory.
  • Cython. This is an extension to Python, which allows one to [conveniently] create compileable snippets for perfomance critical parts of the code.
  • Use a whatever external pre-processor you like. M4 and FilePP are what come to my mind first, but there're plenty of them.
user3159253
  • 16,836
  • 3
  • 30
  • 56
-1
import inspect

frame = inspect.currentframe()
print(f"{frame.f_lineno}\tdebugtext")
l = lambda: frame.f_lineno
print(f"{l()}\tdebugtext")
moken
  • 3,227
  • 8
  • 13
  • 23
Peter-dM
  • 9
  • 1
  • 1
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 15 '23 at 07:02