4

I recently asked a question with title "python find the type of a function" and got very helpful answers. Here is a related question.

Suppose I import *.py files written by me, and these imports result in f being one of the functions defined by me. Now I write to my python interpreter x = f. Later, I want to see the full definition of f, preferably with comments still in place, knowing only x. Is this possible? Does python remember which file the definition was imported from, which is, of course, not enough to give the full definition of f, unless one can find the actual relevant definition?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
David Epstein
  • 433
  • 4
  • 14
  • 1
    in python, variables are only names pointing to objects, so whatever name(s) points to your function, it still points to the exact same object. And functions do know where they were defined. – bruno desthuilliers Jan 03 '19 at 15:52

2 Answers2

5

The built in help(object) will give you the correct documentation if you alias k to some function you commented - same for inspect.getsource(k) - they know which function is ment by your variable name alias k at this time.

See:


Example:

# reusing this code - created it for some other question today

class well_documented_example_class(object):
    """Totally well documented class"""

    def parse(self, message):
        """This method does coool things with your 'message'

        'message' : a string with text in it to be parsed"""
        self.data = [x.strip() for x in message.split(' ')]
        return self.data


# alias for `parse()`:        
k = well_documented_example_class.parse
help(k)

Prints:

Help on function parse in module __main__:

parse(self, message)
    This method does coool things with your 'message'

    'message' : a string with text in it to be parsed

Same goes for inspect.getsource(k):

# from https://stackoverflow.com/a/52333691/7505395
import inspect
print(inspect.getsource(k))

prints:

def parse(self, message):
    """This method does coool things with your 'message'

    'message' : a string with text in it to be parsed"""
    self.data = [x.strip() for x in message.split(' ')]
    return self.data
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

You should think of the way Python uses variables. You have objects (can be classes, functions, lists, scalars or whatelse) and variables that only hold references to those objects.

That explains why when multiple variables point to the same mutable object, if you change it through one of those variables, the change in visible in all other ones.

This is the same thing here. The function object manages all its attributes: its docstring, its code, and its source (if it has: C function show no source). Assigning the function to a new variable does not hide the object behind anything: you still access the original object.

Things would go differently with decorators, because the decorator creates a new object, and the original object is only available to the decorated one.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252