缩进错误python

时间:2010-08-25 19:25:48

标签: python twisted

我正在使用扭曲的API并且正在经历这个例子。 我插入了一个print语句print“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

请有人解释原因吗?

3 个答案:

答案 0 :(得分:1)

看起来你所有功能的“def”前面都有一个空格。在我看来,“def”属于“from”而不是“f”的“r”。

也许如果删除这些空格,问题就会消失。空白对Python很重要。

答案 1 :(得分:0)

检查您是否没有混合空格和标签。

答案 2 :(得分:0)

此错误只能来自错误的缩进。这意味着之前的docstring和print语句有不同的缩进。即使它们看起来正确对齐,也可能混合使用标签和空格。只需重做缩进或复制问题中的代码 - 这样就可以自行修复; - )