tags:

views:

47

answers:

3

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?

+1  A: 

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.

duffymo
I think that's just an SO formatting bug... I've noticed it often before
froadie
yeah.. actually I copied it from webpage and I'm using np++. It didn't make the correct formatting. When I write the code myself it works fine. Thanks
Shwetanka
A: 

Check that you aren't mixing spaces and tabs.

Ned Batchelder
A: 

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

THC4k