If you want to look at the definition of a function in Python but you don’t know where it’s defined, you can access a special attribute of the function
In the following case I have a function imported from
bigram and I want to see what file it’s defined in:
>>> bigram.__globals__['__file__'] '.../python3.7/site-packages/nltk/util.py'
Ah, just as I suspected, it is defined in
__file__ is an attribute of the globals dictionary.
The special attribute
__globals__ is only available for user-defined functions. If you try to access it for a python defined function you’ll get an error:
>>> len.__globals__['__file__'] Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'builtin_function_or_method' object has no attribute '__globals__'