-3

I would like to use let instead of def.

I am looking for sane way of changing syntax of own code in this way.

basically in c it would be

#define let def

How to make the same in python?

cnd
  • 32,616
  • 62
  • 183
  • 313

2 Answers2

4

def is a keyword in python, so it can't be changed to anything else.

From the docs:

The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:

and       del       from      not       while as        elif     
global    or        with assert    else      if        pass      yield
break     except    import    print class     exec      in       
raise continue  finally   is        return def       for       lambda 
try
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    Well, you can write your own parser and produce bytecode. But it is far from clear what the OP is trying to do here. It could be a simple as a regular expression turning `let propername` into `def propername`. We have nothing to go on here though. – Martijn Pieters Jan 16 '13 at 10:49
  • 1
    @MartijnPieters Indeed, but that's a horrible idea. – Ashwini Chaudhary Jan 16 '13 at 10:51
  • then I need syntax synonym to def – cnd Jan 16 '13 at 10:54
  • 2
    @Heather: You can't. Not without extending the Python parser and compiler. – Martijn Pieters Jan 16 '13 at 10:54
  • The quote from the docs doesn't really say that there couldn't be other possibilities. The key point here is, that Python doesn't have any form of a macro system. – phant0m Jan 16 '13 at 13:06
0

#define let def

That would work in C because it is a pre-processor command. Here you are not tricking C, you are tricking the person reading your code.

To achieve the same in Python you need a pre-processor. Like C, you would not be tricking Python you would only be tricking the reader.

So you could write a preprocessor that would be used to launch your program. Like C, you would produce an intermediate file which the "real" python compiler would then use.

I'm not alone in thinking this is not a good idea. This is a support nightmare. You have this preference, but what about everyone who comes after you? How could anyone else maintain this code if you start messing with keywords?

cdarke
  • 42,728
  • 8
  • 80
  • 84