python 缩进错误

发布于 2024-09-16 04:04:35 字数 1179 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韵柒 2024-09-23 04:04:35

看起来所有函数的“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.

于我来说 2024-09-23 04:04:35

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

Check that you aren't mixing spaces and tabs.

や莫失莫忘 2024-09-23 04:04:35

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

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 ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文