简体   繁体   中英

Python #define equivalent

I'm developing a Hebrew python library for my toddler, who doesn't speak English yet. So far I've managed to make it work (function names and variables work fine). The problem is with 'if', 'while', 'for', etc. statements. if this were C++, for ex., I'd use

#define if אם

are there are any alternatives for #define in Python?

****EDIT***** For now a quick and dirty solution works for me; instead of running the program I run this code:

def RunReady(Path):
    source = open(Path, 'rb')
    program = source.read().decode()
    output = open('curr.py', 'wb')

    program = program.replace('כל_עוד', 'while')
    program = program.replace('עבור', 'for')
    program = program.replace('אם', 'if')
    program = program.replace(' ב ', ' in ')
    program = program.replace('הגדר', 'def')
    program = program.replace('אחרת', 'else')
    program = program.replace('או', 'or')
    program = program.replace('וגם', 'and')
    output.write(program.encode('utf-8'))
    output.close()
    source.close()
    import curr

current_file = 'Sapir_1.py'
RunReady(current_file)

Python 3 has 33 keywords of which only a few are used by beginners:

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Given that Python doesn't support renaming keywords it's probably easier to teach a few of these keywords along with teaching programming.

如果你添加#define的东西然后运行c预处理器(但不是编译器),它会给你一个python源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM