简体   繁体   English

Python #define等效

[英]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”,“while”,“for”等语句。 if this were C++, for ex., I'd use 如果这是C ++,例如,我会使用

#define if אם

are there are any alternatives for #define in Python? 在Python中有#define的替代品吗?

****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: Python 3有33个关键字,其中只有少数被初学者使用:

['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. 鉴于Python不支持重命名关键字,可能更容易教授其中一些关键字以及教学编程。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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