Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From CPython source, <code>Objects/frameobject.c</code>:</p> <pre class="lang-c prettyprint-override"><code>static PyMemberDef frame_memberlist[] = { {"f_back", T_OBJECT, OFF(f_back), RO}, {"f_code", T_OBJECT, OFF(f_code), RO}, {"f_builtins", T_OBJECT, OFF(f_builtins),RO}, {"f_globals", T_OBJECT, OFF(f_globals), RO}, {"f_lasti", T_INT, OFF(f_lasti), RO}, {"f_exc_type", T_OBJECT, OFF(f_exc_type)}, {"f_exc_value", T_OBJECT, OFF(f_exc_value)}, {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)}, {NULL} /* Sentinel */ }; ... static PyGetSetDef frame_getsetlist[] = { {"f_locals", (getter)frame_getlocals, NULL, NULL}, {"f_lineno", (getter)frame_getlineno, (setter)frame_setlineno, NULL}, {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL}, {"f_restricted",(getter)frame_getrestricted,NULL, NULL}, {0} }; </code></pre> <p>For the <code>PyMemberDef</code>, the flags <code>RO</code> or <code>READONLY</code> means it's attributes are read-only. For the <code>PyGetSetDef</code>, if it only has a getter, it's read only. This means all attributes but <code>f_exc_type</code>, <code>f_exc_value</code>, <code>f_exc_traceback</code> and <code>f_trace</code> are read-only after creation. This is also mentioned in the docs, under <a href="http://docs.python.org/reference/datamodel.html#index-1767" rel="nofollow noreferrer">Data model</a>.</p> <p>The objects referred to by the attributes is not necessarily read-only. You could do this:</p> <pre><code>&gt;&gt;&gt; f = sys._getframe() &gt;&gt;&gt; f.f_locals['foo'] = 3 &gt;&gt;&gt; foo 3 &gt;&gt;&gt; </code></pre> <p>Though this works in the interpreter, it fails inside functions. The execution engine uses a separate array for local variables (<code>f_fastlocals</code>), which is merged into <code>f_locals</code> on access, but the converse is not true.</p> <pre><code>&gt;&gt;&gt; def foo(): ... x = 3 ... f = sys._getframe() ... print f.f_locals['x'] ... x = 4 ... print f.f_locals['x'] ... d = f.f_locals ... x = 5 ... print d['x'] ... f.f_locals ... print d['x'] ... &gt;&gt;&gt; foo() 3 4 4 5 &gt;&gt;&gt; </code></pre> <p>On the global frame, <code>f_local</code> refers to <code>f_globals</code>, which makes this trick work in the interpreter. Modifying <code>f_globals</code> works, but affects the whole module.</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