def info(object, spacing=10, collapse=1):
"""print methods and doc strings.

Take module, class, list, dictionary, or string."""
methodList = [method for method in dir(object) if callable(getattr(object, method))]
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
print "\n".join(["%s %s" %
processFunc(str(getattr(object, method).__doc__)))
for method in methodList])

if __name__ == "__main__":
print info.__doc__

When I import this source code into Python's IDLE/shell, I receive the error for the respective underlined and bold-ed text of this code:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import apihelper
File "C:\Python31\apihelper.py", line 7
print "\n".join(["%s %s" %
SyntaxError: invalid syntax

What's the correct syntax for this code?

6 Years
Discussion Span
Last Post by d5e5

That cant be the hole line? print "\n".join(["%s %s" % .
Print is a function i python 3,therefor always print() .

Just to finish that line with something so it work

>>> print ("\n".join(["%s %s" % ('a','b')]))
a b
# some test you can look at
>>> print '\n'
  File "<string>", line None
SyntaxError: invalid syntax (<interactive input>, line 1)
>>> print ('\n')

>>> t = ['my', 'car']
>>> print(''.join(t))
>>> print ('I like %s' % ''.join(t))
I like mycar

Edited by snippsat: n/a


You can read the correct version of this code at at this link. I think you have some typing errors in what you show us but it's hard to tell without the code tags.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.