forked from yasoob/intermediatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_introspection.html
More file actions
114 lines (100 loc) · 6.64 KB
/
Copy pathobject_introspection.html
File metadata and controls
114 lines (100 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Object introspection</title>
<link rel="stylesheet" href="_static/epub.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="comprehensions.html" title="Comprehensions"
accesskey="N">next</a></li>
<li class="right" >
<a href="enumerate.html" title="Enumerate"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Python Tips 0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="object-introspection">
<h1>Object introspection</h1>
<p>In computer programming, introspection is the ability to determine the
type of an object at runtime. It is one of Python’s strengths.
Everything in Python is an object and we can examine those objects.
Python ships with a few Built-in functions and modules to help us.</p>
<div class="section" id="dir-bif">
<h2>1.<code class="docutils literal"><span class="pre">dir()</span></code> BIF</h2>
<p>In this section we will learn about <code class="docutils literal"><span class="pre">dir()</span></code> and how it facilitates us
in introspection.</p>
<p>It is one of the most important functions for introspection. It returns
a list of attributes and methods belonging to an object. Here is an
example:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">my_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="nb">dir</span><span class="p">(</span><span class="n">my_list</span><span class="p">)</span>
<span class="c"># Output: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',</span>
<span class="c"># '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',</span>
<span class="c"># '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__',</span>
<span class="c"># '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__',</span>
<span class="c"># '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',</span>
<span class="c"># '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__',</span>
<span class="c"># '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop',</span>
<span class="c"># 'remove', 'reverse', 'sort']</span>
</pre></div>
</div>
<p>Our introspection gave us the names of all the methods of a list. This
can be handy when you are not able to recall a method name. If we run
<code class="docutils literal"><span class="pre">dir()</span></code> without any argument then it returns all names in the current
scope.</p>
</div>
<div class="section" id="type-and-id">
<h2>2.<code class="docutils literal"><span class="pre">type()</span></code> and <code class="docutils literal"><span class="pre">id()</span></code></h2>
<p>The <code class="docutils literal"><span class="pre">type</span></code> function returns the type of an object. For example:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="s">''</span><span class="p">))</span>
<span class="c"># Output: <type 'str'></span>
<span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">([]))</span>
<span class="c"># Output: <type 'list'></span>
<span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">({}))</span>
<span class="c"># Output: <type 'dict'></span>
<span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="nb">bool</span><span class="p">))</span>
<span class="c"># Output: <type 'type'></span>
<span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="c"># Output: <type 'int'></span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">id</span></code> returns the unique ids of various objects. For instance:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">name</span> <span class="o">=</span> <span class="s">"Yasoob"</span>
<span class="k">print</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
<span class="c"># Output: 139972439030304</span>
</pre></div>
</div>
</div>
<div class="section" id="inspect-module">
<h2>3.<code class="docutils literal"><span class="pre">inspect</span></code> module</h2>
<p>The inspect module also provides several useful functions to get
information about live objects. For example you can check the members of
an object by running:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">inspect</span>
<span class="k">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getmembers</span><span class="p">(</span><span class="nb">str</span><span class="p">))</span>
<span class="c"># Output: [('__add__', <slot wrapper '__add__' of ... ...</span>
</pre></div>
</div>
<p>There are a couple of other methods as well which help in introspection.
You can explore them if you wish.</p>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2015, Muhammad Yasoob Ullah Khalid.
</div>
</body>
</html>