How can I read an imported object's code?
<p>I don't know what attributes an object supports. How can I open up the module and figure out what's going on "under the hood"?</p>

<p>For example, I want to figure out what attributes the <code>datetime</code> object from the <code>datetime</code> module has in its <code>__init__</code>:</p>

<pre><code>import datetime
curtime = datetime.datetime.now()
print(curtime[0])
</code></pre>

<p>But, since <code>datetime</code> has no <code>__getitem__</code> method, there must be a different way of accessing each part of the time (<code>day</code>, <code>month</code>, <code>year</code>, etc.)</p>

<p>I have found (by trial and error) that you can do this:</p>

<pre><code>datetime.datetime.now().time()
datetime.datetime.now().date()
</code></pre>

<p>but I'm not sure what the proper name for what I'm doing here is, and how I can find other methods that belong to this object/class.</p>

<p>I find the <code>dir</code> function to be the most useful when dealing with unknown and undocumented objects (<a href="https://docs.python.org/2/library/datetime.html" rel="nofollow"><code>datetime</code> is well documented</a>, btw).</p>

<p>But if it weren't documented:</p>

<pre><code>&gt;&gt;&gt; import datetime
&gt;&gt;&gt;
&gt;&gt;&gt; dir(datetime)
['MAXYEAR', 'MINYEAR', '__doc__', '__name__', '__package__', 'date', 
'datetime', 'datetime_CAPI', 'time', 'timedelta', 'tzinfo']
&gt;&gt;&gt; datetime.date
&lt;type 'datetime.date'&gt;
&gt;&gt;&gt; dir(datetime.date)
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
'__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__',
'__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__',
'__subclasshook__', 'ctime', 'day', 'fromordinal', 'fromtimestamp',
'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month', 'replace',
'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday',
'year']
</code></pre>

<p>and so on...</p>

python