저는 파이썬을 처음 접했습니다. 무한 루프에서 실행되도록 스크립트를 수정하려고합니다. 콘솔에서 파이썬 코드 행을 가져 와서 파이썬 코드 행을 실행합니다. 나는 간부() 함수를 사용하려고했습니다파이썬 명령을 가져오고 실행할 수있는 파이썬 스크립트를 작성하는 방법은 무엇입니까?
Shell> myconsole.py
> PredefindFunction ("Hello")
This is the result of the PredefinedFunction: Hello!!!
> a=1
> if a==1:
> print "a=1"
a=1
> quit
Shell>
:
나는 다음과 같은 예를 할 수있는 일에 대해 이야기하고있다. 스크립트에서 정의한 함수를 실행하면 잘 작동하지만 실제로 어떤 이유로 든 모든 코드를 실행할 수는 없습니다. 나는 그 논리를 얻지 못한다. 알 겠어 :
Shell> myconsole.py
> PredefindFunction ("Hello")
This is the result of the PredefinedFunction: Hello!!!
> a=1
> print a
...
NameError: name 'a' is not defined
Shell>
아무도 도와 줄 수 있습니까? 사용자 정의 파이썬 쉘을 구축을 위해 노력하고있는 것처럼
class cParseTermCmd:
def __init__(self, line = ""):
self.TermPrompt = "t>"
self.oTermPrompt = re.compile("t>", re.IGNORECASE)
self.TermCmdLine = ""
self.line = line
# Check if the TermPrompt (t>) exist in line
def mIsTermCmd (self):
return self.oTermPrompt.match(self.line)
# Remove the term prompt from the terminal command line
def mParseTermCmd (self):
self.TermCmdLine = re.sub(r'%s'%self.TermPrompt, '', self.line, flags=re.IGNORECASE)
exec (self.TermCmdLine)
And I call it in an infinite while loop from:
def GetCmd (self):
line = raw_input('>')
self.TermCmdLine = cParseTermCmd(line)
if self.TermCmdLine.mIsTermCmd():
# Execute terminal command
self.TermCmdLine.mParseTermCmd()
else:
return line
지금까지 해본 코드 중 일부를 게시 할 수 있습니까? 나는이 두 라인'exec ('a = 2')로 문제가있는 범위를 얻습니다. print (a)' –
안녕 카일, 질문에 추가했습니다. 감사! –