参数未正确传递

时间:2010-02-27 12:17:23

标签: python eval

以下是我的代码的摘录:

def listFrom(here):
    print "[DBG] here: " + here

def book(here, there, amount):
    print "[DBG] here: " + here + "; there: " + there + "; amount: " + str(amount)

# Code that takes input and stores it into the string input

# Yes, I know this is dangerous, but it's part of a
# school assignment where we HAVE to use eval.
eval(input, {"__builtins__": {}, "listAll": listAll, "listFrom": listFrom, "listFromTo": listFromTo, "book": book, "about": about, "commands": commands, "book": book})

如果我输入listFrom('LON'),程序会按预期返回[DBG] here: LON。但是,当我book('LON', 'MAN', 8)时,我得到一个莫名其妙的[DBG] here: ☺; there: ☻; amount: ♥。可能是什么原因造成的?

1 个答案:

答案 0 :(得分:0)

此代码在Linux / x86-32上的Python 2.6中没有问题:

>>> def listFrom(here):
...     print "[DBG] here: " + here
... 
>>> def book(here, there, amount):
...     print "[DBG] here: " + here + "; there: " + there + "; amount: " + str(amount)
... 
>>> book('LON', 'MAN', 8)
[DBG] here: LON; there: MAN; amount: 8
>>> input = """book('LON', 'MAN', 8)"""
>>> eval(input, {"__builtins__": {}, "listFrom": listFrom, "book": book})
[DBG] here: LON; there: MAN; amount: 8
>>> eval("""listFrom('LON')""", {"__builtins__": {}, "listFrom": listFrom, "book": book})
[DBG] here: LON

您使用的是哪个Python版本?在哪个OS /架构上?