2010-02-27 1 views
0

여기 내 코드의 발췌입니다 통과 가져 오지 : 나는 listFrom('LON')를 입력하면 예상대로매개 변수가 제대로

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}) 

이 프로그램은 [DBG] here: LON를 반환합니다. 그러나, 내가 할 때 book('LON', 'MAN', 8)는 설명 할 수없는 것을 얻는다 [DBG] here: ☺; there: ☻; amount: ♥. 이것의 원인이 무엇일까요?

+0

출력을 어디에서보고 있습니까? IDLE 세션을 게시 할 수 있습니까? 그렇지 않으면 복제가 수행되지 않습니다. – SilentGhost

+0

복제 할 수 없습니다. 어떤 Python과 어떤 플랫폼입니까? –

+0

나를 위해 잘 작동하는 것 같습니다. 사용자 입력을 위해 사용중인 코드를 게시 할 수 있습니까? 또한 문자열 변수를'input'이 아닌 다른 함수라고 부릅니다. 이것은 내장 함수 중 하나입니다. 어떤 Python 버전을 사용하고 있습니까? – tzaman

답변

0

이 코드는 리눅스/x86-32에 파이썬 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 

당신이 사용하는 어떤 파이썬 버전? 어떤 OS/아키텍처에서?

+0

아, 네가 맞아. 내가 격리 한 코드는 잘 동작하지만, 어떤 경우에는 'eval'이 전혀 실행되지 않는 이상한 일을했다. – Pieter