2

python code:

def fun(a,b):
   c=a+b
   return c

sed command to add doc string:

sed -i '/^def/a \\t """\n\tSummary:\n\t-------\n\tParameters:\n\t----------\n\tReturn:\n\t------\n\t"""' filename

python code after sed command

def fun(a,b):
   """
   Summary:
   -------
   Parameters:
   -----------
   Return:
   -------
   """
   c=a+b
   return c

Now the problem is added doc-string looks correct in vim editor but on running a code execution hits indentation error. Can anyone explain what wrong is happening here?

TheBeginner
  • 405
  • 5
  • 23
  • Don't mix tabs & spaces for indentation. It's not easy to get it right, unless you understand exactly how Python interprets tabs. And in Python 3, it can cause an error to mix tabs & spaces for indentation. In PEP-8 it's recommended to just use spaces, with 4 spaces per indentation level. If you really must use tabs, you can read the rules here. https://stackoverflow.com/a/2034527/4014959 – PM 2Ring Sep 27 '18 at 08:48

1 Answers1

1

Your sed command inserts the docstring indented with tabs.

It seems like the rest of your code is indented with spaces. Therefore it may look correct in the vim editor but the python interpreter sees different levels of indentation.

You can use cat -t yourFile.py to if you mixed up tabs and spaces.

If the output looks like this

def fun(a,b):
^I"""
^ISummary:
^I-------
^IParameters:
^I-----------
^IReturn:
^I-------
^I"""
    c=a+b
    return c

you mixed up tabs and spaces. (^I represents a tab)

In that case replace the tabs in your sed command with the spaces.

kalehmann
  • 4,821
  • 6
  • 26
  • 36