Skip to content

Commit 36522f9

Browse files
committed
List, operator and introspection
1 parent 9ac6049 commit 36522f9

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ flatter_list = list(itertools.chain.from_iterable(<list>))
5353
product_of_elems = functools.reduce(lambda out, x: out * x, <collection>)
5454
list_of_chars = list(<str>)
5555
```
56+
* **Check out module [operator](#operator) for alternative versions of examples.**
5657

5758
```python
5859
<int> = <list>.count(<el>) # Returns number of occurrences. Also works on strings.
@@ -2112,10 +2113,10 @@ from operator import itemgetter, attrgetter, methodcaller
21122113

21132114
```python
21142115
import operator as op
2115-
product_of_elems = functools.reduce(op.mul, <collection>)
21162116
elementwise_sum = map(op.add, list_a, list_b)
21172117
sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
21182118
sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
2119+
product_of_elems = functools.reduce(op.mul, <collection>)
21192120
LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_})
21202121
last_el = op.methodcaller('pop')(<list>)
21212122
```
@@ -2127,15 +2128,15 @@ Introspection
21272128

21282129
### Variables
21292130
```python
2130-
<list> = dir() # Returns names of local variables (including functions).
2131+
<list> = dir() # Returns names of local variables (incl. functions).
21312132
<dict> = vars() # Returns dict of local variables. Also locals().
21322133
<dict> = globals() # Returns dict of global variables.
21332134
```
21342135

21352136
### Attributes
21362137
```python
21372138
<list> = dir(<object>) # Returns names of object's attributes (incl. methods).
2138-
<dict> = vars(<object>) # Returns dict of object's fields. Also <object>.__dict__.
2139+
<dict> = vars(<object>) # Returns dict of object's fields. Also <obj>.__dict__.
21392140
```
21402141

21412142
```python

index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@
249249
product_of_elems = functools.reduce(<span class="hljs-keyword">lambda</span> out, x: out * x, &lt;collection&gt;)
250250
list_of_chars = list(&lt;str&gt;)
251251
</code></pre>
252+
<ul>
253+
<li><strong>Check out module <a href="#operator">operator</a> for alternative versions of examples.</strong></li>
254+
</ul>
252255
<pre><code class="python language-python hljs">&lt;int&gt; = &lt;list&gt;.count(&lt;el&gt;) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span>
253256
index = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns index of first occurrence or raises ValueError.</span>
254257
&lt;list&gt;.insert(index, &lt;el&gt;) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
@@ -1853,22 +1856,22 @@
18531856

18541857

18551858
<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> operator <span class="hljs-keyword">as</span> op
1856-
product_of_elems = functools.reduce(op.mul, &lt;collection&gt;)
18571859
elementwise_sum = map(op.add, list_a, list_b)
18581860
sorted_by_second = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hljs-number">1</span>))
18591861
sorted_by_both = sorted(&lt;collection&gt;, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
1862+
product_of_elems = functools.reduce(op.mul, &lt;collection&gt;)
18601863
LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span> : op.or_})
18611864
last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(&lt;list&gt;)
18621865
</code></pre>
1863-
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Returns names of local variables (including functions).</span>
1866+
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Returns names of local variables (incl. functions).</span>
18641867
&lt;dict&gt; = vars() <span class="hljs-comment"># Returns dict of local variables. Also locals().</span>
18651868
&lt;dict&gt; = globals() <span class="hljs-comment"># Returns dict of global variables.</span>
18661869
</code></pre></div></div>
18671870

18681871

18691872

18701873
<div><h3 id="attributes-1">Attributes</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir(&lt;object&gt;) <span class="hljs-comment"># Returns names of object's attributes (incl. methods).</span>
1871-
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Returns dict of object's fields. Also &lt;object&gt;.__dict__.</span>
1874+
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Returns dict of object's fields. Also &lt;obj&gt;.__dict__.</span>
18721875
</code></pre></div>
18731876

18741877
<pre><code class="python language-python hljs">&lt;bool&gt; = hasattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>)

0 commit comments

Comments
 (0)