Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By Python's rules, you cannot alter your caller's <code>locals</code>; in the current implementations, if you try (e.g. with the black magic Anurag suggests) you will not get an exception (though I'd like to add that error check to some future version), but it will be essentially inoperative if your caller is a function (not if your caller is module top-level code) -- the caller's actual local variables won't in fact be affected. This holds whether the caller's <code>locals</code> are explicitly passed in, or fetched through black magic: they still need to be treated as a read-only dict if your code is to have any sanity.</p> <p>Rather, you could have the caller pass in an explicit, real, normal dict (which could be initialized from <code>locals()</code> if you want), and all alterations your code does in that dict will still be there for the caller's use -- just not as "new barenames" in the caller's local scope of course, but the functionality is the same whether the caller needs to use <code>x['foo']</code> or <code>x.foo</code> or (as you'd prefer) just barename <code>foo</code>.</p> <p>BTW, to use attribute-access syntax rather than dict indexing syntax, you can do:</p> <pre><code>class Bunch(object): pass ... # caller code b = Bunch() thefun(b) print b.foo ... # called function def thefun(b): b.foo = 23 </code></pre> <p>This also covers, with a tiny variation, the case in which <code>thefun</code> wants to work with dict indexing syntax (say its body is <code>b['foo'] = 23</code> instead of <code>b.foo = 23</code>): in that case, the caller just needs to use <code>thefun(vars(b))</code> instead of the plain <code>thefun(b)</code>, but it can keep working with the <code>b.foo</code> access syntax afterwards.</p>
    singulars
    1. This table or related slice is empty.
    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