如何重命名内置函数,例如将"if"重命名为"hehe","elif"重命名为"haha","else"重命名为"hihi"?

3

我该如何更改函数名称,以便Python可以将hehe读作if,haha读作elif,hihi读作else。 当前的代码大致如下:

if x == 7 or x == 2:
    print(":)")
elif x == 3:
    print(":(")
else:
    print(":|")

我希望我的代码像这样:

hehe x == 7 or x == 2:
    print(":)")

haha x == 3:
    print(":(")
hihi:
    print(":|")

我希望将每个函数的名称更改以使代码完全无法阅读。这没有实际用途,我只是出于兴趣而这么做。 我尝试编写一个编译器,并且已经得到了类似basic的东西,但我想在Python中实现它,而不创建编译器。 可以使用字典来解决问题,格式如下:
dict = {
"if": "hehe"
"elif": "haha"
"else": "hihi"
}

但我不知道如何在代码中使其工作,这样我才能在这个“新语言”中编写代码。


看看这个是否有帮助:https://dev59.com/62Ij5IYBdhLWcg3wKiHs - Hannon qaoud
1
只是为了明确,您想要能够编写并运行新语法,还是想要一个单独的脚本来根据您的标准对现有的Python代码进行编码? - Hannon qaoud
2
if 不是一个函数,而是一个关键字。更改关键字需要对文件进行单独的处理,不能轻易地在 Python 解释器内部完成:您需要创建一个包装脚本或者使用编码注册表来进行黑魔法操作。 - MegaIng
这让事情变得更清晰了,我看到一些人用JS做了同样的事情,只是使用了一个字典和大约50行代码。现在我至少有了明确的方向。 - Georxy
1
@CharlesDuffy 我会默认这只是一个简化的例子,而不是实际目标。也许实际目标是翻译关键字或类似的东西。 - MegaIng
显示剩余4条评论
1个回答

2

我没有花太多时间在这个上面(它可能需要改进),这是我第一次使用tokenize模块,但这是我的成果。

当我在寻找Python解析器时,我发现了这个模块,它基本上解析Python代码并对其进行分类,从那里你可以按照自己的需求进行操作。

from token import DEDENT, INDENT, NEWLINE
import tokenize
result = ''

names = {
    'if': 'hehe',
    'elif': 'haha',
    'else': 'hihi',
    # Add here all the other names, that you want to change, and don't worry about a name occurring inside a string it will not be changed
}
# open the script you want to encrypt in a tokenize file object
with tokenize.open('z.py') as f:
    # create a new tokenize object and feed it the lines
    tokens = tokenize.generate_tokens(f.readline)
    # for every token in all the tokens in the file:
    for token in tokens:
        if names.get(token[1]): # token[1] is the string of the token i.e 'if', 'for', '\n', 'or' etc
            result += names.get(token[1]) + ' '
        elif token.type == NEWLINE or token[1] == INDENT or token.type == DEDENT:
            result += token[1]
            print(result)
        else:
            result += token[1] + ' '


with open('z.py', 'w') as f:
    f.write(result)

更新

之前的代码只能进行编码,通过一些小的修改,您可以重复使用相同的代码进行解码和编码脚本:

from token import DEDENT, INDENT, NEWLINE
import tokenize

encode_name = {
    'if': 'hehe',
    'elif': 'haha',
    'else': 'hihi',
}

def code(name, encode=True):
    if encode:
        names = name
    else:
        # flip the dict, keys become values and vice versa
        names = {v: k for k, v in name.items()}
    
    result = ''
    with tokenize.open('z.py') as f:
        tokens = tokenize.generate_tokens(f.readline)
        for token in tokens:
            if names.get(token[1]):
                result += names.get(token[1]) + ' '
            elif token.type == NEWLINE or token[1] == INDENT or token.type == DEDENT:
                result += token[1]
            else:
                result += token[1] + ' '

    with open('z.py', 'w') as f:
        f.write(result)


code(encode_name, encode = False)

查看官方文档获取更多信息,虽然我自己不是专家,但请随时在此处提问。

非常乐意帮忙 祝你好运,编码愉快


谢谢!但是我该如何在这种“语言”中编写代码并使其像正常代码一样工作呢?您能帮我解码过程吗? :D 但是我希望能够使用它导入模块并开始在这种语言中编写。 - Georxy
更新以进行解码和编码,请确保标记最正确的答案 :)。 - Hannon qaoud
1
现在我们回到完全改变Python行为的话题,这不是我的领域,我也不知道是否可能从一个模块开始,你正在谈论创建一个全新的个性化Python副本,这超出了我、你和整个网站的能力范围,祝你的任务好运。 - Hannon qaoud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接