Browse Source

Introspection

pull/57/head
Jure Šorn 4 years ago
parent
commit
92695eb654
2 changed files with 3 additions and 7 deletions
  1. 3
      README.md
  2. 7
      index.html

3
README.md

@ -2142,9 +2142,6 @@ Introspection
```python
<list> = dir(<object>) # Names of object's attributes (incl. methods).
<dict> = vars(<object>) # Dict of object's fields. Also <obj>.__dict__.
```
```python
<bool> = hasattr(<object>, '<attr_name>') # Checks if getattr() raises an error.
value = getattr(<object>, '<attr_name>') # Raises AttributeError if attribute is missing.
setattr(<object>, '<attr_name>', value) # Only works on objects with __dict__ attribute.

7
index.html

@ -1875,13 +1875,12 @@ last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(&lt;l
<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"># Names of object's attributes (incl. methods).</span>
&lt;dict&gt; = vars(&lt;object&gt;) <span class="hljs-comment"># Dict of object's fields. Also &lt;obj&gt;.__dict__.</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;bool&gt; = hasattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Checks if getattr() raises an error.</span>
&lt;bool&gt; = hasattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Checks if getattr() raises an error.</span>
value = getattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Raises AttributeError if attribute is missing.</span>
setattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>, value) <span class="hljs-comment"># Only works on objects with __dict__ attribute.</span>
delattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>) <span class="hljs-comment"># Equivalent to `del &lt;object&gt;.&lt;attr_name&gt;`.</span>
</code></pre>
</code></pre></div>
<div><h3 id="parameters-1">Parameters</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> inspect <span class="hljs-keyword">import</span> signature
&lt;sig&gt; = signature(&lt;function&gt;)
no_of_params = len(&lt;sig&gt;.parameters)

Loading…
Cancel
Save