python 缩进错误
我正在使用twisted API并且正在经历这个例子。 我插入了一条带有正确缩进的打印语句“in getdummydata”。代码如下:
from twisted.internet import reactor, defer
def getDummyData(x):
"""
This function is a dummy which simulates a delayed result and
returns a Deferred which will fire with that result. Don't try too
hard to understand this.
"""
print "in getdummydata"
d = defer.Deferred()
# simulate a delayed result by asking the reactor to fire the
# Deferred in 2 seconds time with the result x * 3
reactor.callLater(2, d.callback, x * 3)
return d
def printData(d):
"""
Data handling function to be added as a callback: handles the
data by printing the result
"""
print d
d = getDummyData(3)
d.addCallback(printData)
# manually set up the end of the process by asking the reactor to
# stop itself in 4 seconds time
reactor.callLater(4, reactor.stop)
# start up the Twisted reactor (event loop handler) manually
reactor.run()
但是当我运行代码时,它给出了以下缩进错误:
File "C:\Python26\programs\twisttest.py", line 9
print "in getdummydata"
^
IndentationError: unexpected indent
请问有人可以解释为什么吗?
I'm using twisted API and was going through this example.
I inserted one print statement print "in getdummydata" with correct indentation. code is as below:
from twisted.internet import reactor, defer
def getDummyData(x):
"""
This function is a dummy which simulates a delayed result and
returns a Deferred which will fire with that result. Don't try too
hard to understand this.
"""
print "in getdummydata"
d = defer.Deferred()
# simulate a delayed result by asking the reactor to fire the
# Deferred in 2 seconds time with the result x * 3
reactor.callLater(2, d.callback, x * 3)
return d
def printData(d):
"""
Data handling function to be added as a callback: handles the
data by printing the result
"""
print d
d = getDummyData(3)
d.addCallback(printData)
# manually set up the end of the process by asking the reactor to
# stop itself in 4 seconds time
reactor.callLater(4, reactor.stop)
# start up the Twisted reactor (event loop handler) manually
reactor.run()
But when I run the code it gives the indentation error below:
File "C:\Python26\programs\twisttest.py", line 9
print "in getdummydata"
^
IndentationError: unexpected indent
Please can anyone explain why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来所有函数的“def”前面都有一个空格。在我看来,“def”属于上面“from”中的“r”,而不是“f”。
也许如果你删除这些空格,问题就会消失。空格对于 Python 来说很重要。
It looks like the "def" for all your functions have one blank space in front of them. By my eye, "def" falls under the "r" in the "from" above rather than the "f".
Perhaps if you remove those spaces the problem will go away. Whitespace is important to Python.
检查您是否没有混合使用空格和制表符。
Check that you aren't mixing spaces and tabs.
此错误只能来自错误的缩进。这意味着之前的文档字符串和打印语句具有不同的缩进。即使它们看起来正确对齐,也可能存在制表符和空格的混合。只需重做缩进或复制问题中的代码 - 所以可以自行修复;-)
This error can only come from wrong indentation. This means that the docstring before and the print statement have different indentation. Even if they look properly aligned there might be a mix of tabs and spaces. Just redo the indentation or copy the code from your question - SO fixed it by itself ;-)