简体   繁体   English

缩进错误python

[英]Indentation Error python

I'm using twisted API and was going through this example. 我正在使用扭曲的API,并正在通过此示例。 I inserted one print statement print "in getdummydata" with correct indentation. 我在带有正确缩进的“ getdummydata”中插入了一个打印语句打印。 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? 请谁能解释为什么?

It looks like the "def" for all your functions have one blank space in front of them. 看起来所有函数的“ def”前面都有一个空格。 By my eye, "def" falls under the "r" in the "from" above rather than the "f". 在我看来,“ def”位于“从”上方的“ r”之下,而不是“ f”之下。

Perhaps if you remove those spaces the problem will go away. 也许如果您删除这些空格,问题将消失。 Whitespace is important to Python. 空格对于Python很重要。

检查您没有混合使用空格和制表符。

This error can only come from wrong indentation. 此错误只能来自错误的缩进。 This means that the docstring before and the print statement have different indentation. 这意味着before的文档字符串和print语句具有不同的缩进。 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 ;-) 只需重做缩进或从您的问题中复制代码-即可自行修复;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM