def traceit(frame, event, trace_arg):
global stepping
if event == 'line':
if stepping or frame.f_lineno in breakpoints:
resume = False
while not resume:
print(event, frame.f_lineno, frame.f_code.co_name, frame.f_locals)
command = input_command()
resume = debug(command, frame.f_locals)
return traceit
代码中最后一行是什么意思?
编辑:
def remove_html_markup(s):
tag = False
quote = False
out = ""
for c in s:
if c == '<' and not quote:
tag = True
elif c == '>' and not quote:
tag = False
elif c == '"' or c == "'" and tag:
quote = not quote
elif not tag:
out = out + c
return out
def main():
print (remove_html_markup('xyz'))
print (remove_html_markup('"<b>foo</b>"'))
print (remove_html_markup("'<b>foo</b>'"))
# globals
breakpoints = {9: True}
stepping = False
def debug(command, my_locals):
global stepping
global breakpoints
if command.find(' ') > 0:
arg = command.split(' ')[1]
else:
arg = None
if command.startswith('s'): # step
stepping = True
return True
elif command.startswith('c'): # continue
stepping = False
return True
elif command.startswith('q'): # quit
sys.exit(0)
else:
print ("No such command", repr(command))
return False
commands = ['s', 's', 's', 'q']
def input_command():
#command = raw_input("(my-spyder) ")
global commands
command = commands.pop(0)
return command
def traceit(frame, event, trace_arg):
global stepping
if event == 'line':
if stepping or frame.f_lineno in breakpoints:
resume = False
while not resume:
print(event, frame.f_lineno, frame.f_code.co_name, frame.f_locals)
command = input_command()
resume = debug(command, frame.f_locals)
return traceit
# Using the tracer
sys.settrace(traceit)
main()
sys.settrace(None)
发布于 2014-08-08 21:43:38
因为Python中的所有函数都是作为对象创建的,所以它返回对函数的引用。
它可以在稍后的代码中传递到另一个函数中,或者使用参数调用,就像对任何函数一样。
def a(str):
print str
b = a # Assign an instance of a to b
b('hello') # Call b as if it were a
print type(b)
打印:
hello
<type 'function'>
发布于 2014-08-08 21:45:06
它返回一个函数对象。我很好奇您是否在活动代码中发现了这一点,以及用例可能是什么。
https://stackoverflow.com/questions/25204896
复制相似问题