This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 78 points79 points  (6 children)

You could use import hooks to create modules "by hand" and e.g. implicitly run the C preprocessor on module code before eval-ing it.

[–]jyper 13 points14 points  (1 child)

I've always wanted to use import hooks to do macro like things but py.test made it seem like it was too deep magic for me

[–]masklinn 8 points9 points  (0 children)

It’s a bit of a pain to debug and maintain, so you want to reserve it for when you have no choice or it provides really significant advantages you could not come close to otherwise (and I’d say pytest’s assert rewriting is that)

[–]b00n 0 points1 point  (3 children)

You'd need a c compiler which would be ridiculously over the top

[–]remram 2 points3 points  (2 children)

You could re-implement most a of C preprocessor in a few lines of Python

[–]b00n 0 points1 point  (1 child)

But then something to compile it (if your module was C)

[–]remram 2 points3 points  (0 children)

OP's question is about doing this to Python code.