Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do that "function accessing its own attributes" correctly for all situations - see for details here <a href="https://stackoverflow.com/questions/3109289/how-can-python-function-access-its-own-attributes">how can python function access its own attributes?</a> - but here is a quick demonstration:</p> <pre><code>&gt;&gt;&gt; def f(): return f.x ... &gt;&gt;&gt; f.x = 7 &gt;&gt;&gt; f() 7 &gt;&gt;&gt; g = f &gt;&gt;&gt; g() 7 &gt;&gt;&gt; del f &gt;&gt;&gt; g() Traceback (most recent call last): File "&lt;interactive input&gt;", line 1, in &lt;module&gt; File "&lt;interactive input&gt;", line 1, in f NameError: global name 'f' is not defined </code></pre> <p>Basically most methods directly or indirectly rely on accessing the function object through lookup by name in globals; and if original function name is deleted, this stops working. There are other kludgey ways of accomplishing this, like defining class, or factory - but thanks to your explanation it is clear you don't really need that.</p> <p>Just do the mentioned keyword catch-all argument, like so:</p> <pre><code>def fn1(oneArg): // do the due def fn2(oneArg, **kw): if 'option1' in kw: print 'called with option1=', kw['option1'] //do the rest fn2(42) fn2(42, option1='something') </code></pre> <p>Not sure what you mean in your comment of handling TypeError - that won't arise when using **kw. This approach works very well for some python system functions - check min(), max(), sort(). Recently <code>sorted(dct,key=dct.get,reverse=True)</code> came very handy to me in CodeGolf challenge :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload