In Python application, can't find where a item comes from
<p>I'm trying to understand how web the framework <strong>web2py</strong> works, and there is this item called <code>request.client</code> in <code>access.py</code> that I can't figure out where it comes from. I retrieved the code from <a href="https://github.com/web2py/web2py/blob/0d646fa5e7c731cb5c392adf6a885351e77e4903/applications/admin/models/access.py" rel="nofollow">here</a> and it begins as follows:</p>

<pre><code>import base64
import os
import time
from gluon.admin import apath
from gluon.fileutils import read_file
from gluon.utils import web2py_uuid
from pydal.contrib import portalocker
# ###########################################################
# ## make sure administrator is on localhost or https
# ###########################################################


http_host = request.env.http_host.split(':')[0]
</code></pre>

<p>My question is: when we don't know from where an item comes in a code, what is the method to find it out ? </p>

<p>The file to which you link, <code>access.py</code>, mentions the name <code>request</code> for the first time on line 13:</p>

<pre><code>http_host = request.env.http_host.split(':')[0]
</code></pre>

<p>This symbol, <code>request</code>, is not <code>import</code>ed from anywhere.  This is an unorthodox way of writing Python programs in general, and in isolation it looks impossible. But it <em>is</em> possible, if the symbol <code>request</code> was added to the global workspace of this file by some other file that wraps the execution of <code>access.py</code>. Here is a minimal example:</p>

<pre><code>d = { 'request' : some_object() }
execfile( 'access.py', d )  # run the named file using d as its global workspace
</code></pre>

<p>So, in this particular case the way to investigate is to find the file that executes <code>access.py</code>. </p>

<p>[ In response to your comment about examining every file in the library: for such an analysis, and in general for a better quality of life as a programmer, you <em>will</em> need some tool that can search for a particular string in a whole hierarchy of files (like the command-line tool <code>grep</code> on Linux and MacOS,  or like many third-party text editors and IDEs on Windows, but <strong><em>not</em></strong> the perennially unreliable Windows "Search" function that claims to be able to do this). ]</p>

<p>In answer to your more general question about finding where things come from:  The other answers and comments mention attributes like <code>__file__</code> and/or <code>__module__</code>. These can help if you have a way of making your code spit out debug information.  Since you're working with a non-console (web) framework, that may be non-trivial, but one quick-and-dirty way might be to insert a line like this in <code>access.py</code>:</p>

<pre><code>open('some_temporary_file_somewhere.txt', 'wt').write('\n'.join([
    repr( request ),
    request.__module__,
    request.__class__,
])
</code></pre>

<p>This should get you the details of where <code>request</code>'s class was defined (but not necessarily the name of the file in which the actual instance named <code>request</code> was created).</p>

python web2py