2010-08-25 83 views
0

我正在使用扭曲的API并正在通过这个例子。 我插入了一个打印语句打印“在getdummydata”正确的缩进。代码如下:缩进错误python

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 

请任何人都可以解释,为什么?

+2

工作对我来说很好,正如预期的那样。确保你到处都有4个空格,并且没有标签。 – delnan 2010-08-25 19:30:04

回答

1

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

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

+1

我认为这只是一个SO格式化错误...我已经注意到它经常在 – froadie 2010-08-25 19:30:02

+0

是啊..其实我从网页复制它,我使用np ++。它没有做出正确的格式。当我自己编写代码时,它工作正常。谢谢 – Shwetanka 2010-08-25 19:40:18

0

检查您是否混合了空格和制表符。

0

此错误只能来自错误的缩进。这意味着之前的文档字符串和print语句具有不同的缩进。即使它们看起来正确对齐,也可能会混合使用制表符和空格。只需重做缩进或从你的问题复制代码 - 所以它自己修复它;-)