-6

This would be fairly easy in Java since there are curly braces that are required for beginning and ending that code block and so I could write a program to look for the curly braces and label each function.

The problem I'm having with Python is finding some sort of marker that tells me when a function ends. I'm guessing one solution would be to figure out some scheme to detect the '\t' for each line. What are some methods for determining the beginning of a function block and it's end?

MBT
  • 21,733
  • 19
  • 84
  • 102
  • 2
    you already give the answer in your own question... so why do you ask? – Julien Nov 14 '17 at 04:14
  • Are you asking about counting the lines in python source code, or are you asking about obtaining a function's line count _during runtime_, i.e. `def linecount(function): return line_count_of_function`? – Aran-Fey Nov 14 '17 at 11:22

1 Answers1

0

blanks are generally preferred to tabs, and mixing is definitely not acceptable. regardless, functions can also be defined inside of other functions and in classes, so your q might be a bit more subtle.

(btw, while in the REPL a blank line will end a def, that's not the case in general.)

you might be able to leverage the inspect module

In [1]: import inspect

In [2]: def h():
    ...:     a =9
    ...:     b =8
    ...:     return a+b
    ...: 

In [3]: inspect.getsourcelines(h)
Out[3]: ([u'def h():\n', u'    a =9 \n', u'    b =8\n', u'    return a+b\n'], 1)

In [4]: inspect.getsourcelines(h)[0]
Out[4]: [u'def h():\n', u'    a =9\n', u'    b =8\n', u'    return a+b\n']

In [4]: len(inspect.getsourcelines(h)[0])
Out[4]: 4
ShpielMeister
  • 1,417
  • 10
  • 23