-3

In C, one can use #define macros to keep the definition of expressions in one place to provide a single source of truth. Is there anything similar in python that will allow me to create macros?

Ryan Jensen
  • 207
  • 1
  • 9
  • 1
    Not from what I know of. – shahkalpesh Dec 31 '18 at 12:29
  • 4
    Remember that even your example is strictly speaking not C code but pre-processor instructions. Python does not have a pre-processor and does not allow to rename keywords. Even if it would be possible it would violate many of the code principles Python is built around. – Klaus D. Dec 31 '18 at 12:32
  • @KlausD., Can you give a few examples of the "code principals" around which Python is built, and how my idea would break them? – Ryan Jensen Dec 31 '18 at 14:09
  • https://www.python.org/dev/peps/pep-0020/ – Klaus D. Dec 31 '18 at 14:24
  • @KlausD. If I were to rename "elif" to "ef" and rename "else" to "el", that would make more control keywords 2 characters long (if, ef, el). It would improve the beauty and readability of the code. That goal is in alignment with the first line of the poem you referenced. – Ryan Jensen Dec 31 '18 at 15:51

2 Answers2

1

Python is a very permissive language but there are some reserved keywords.

Here is a description how you can simulate c preprocessor.

Just for fun in version 2.x you may even swap True and False. Check here.

Jónás Balázs
  • 781
  • 10
  • 24
0

What you are referring to is preprocessing the source code. In C (and some other languages), you can do simple find and replace operations on the source code, treating the source code as simple text.

Python does not ship with anything like that. However, you can create variables that refer to globals. For instance, you could create a variable j that is the same as os.path.join like this:

 import os
 j = os.path.join

 print(j("one", "two")) # same as print(os.path.join("one", "two"))
Flimm
  • 136,138
  • 45
  • 251
  • 267