Note that there are some explanatory texts on larger screens.

plurals
  1. POUse a class in the context of a different module
    primarykey
    data
    text
    <p>I want to modify some classes in the standard library to use a different set of globals the ones that other classes in that module use.</p> <h2>Example</h2> <p>This example is an example only:</p> <pre><code># module_a.py my_global = [] class A: def __init__(self): my_global.append(self) class B: def __init__(self): my_global.append(self) </code></pre> <p>In this example, If I create an instance of <code>A</code>, via <code>A()</code>, it will call <code>append</code> on the object named by <code>my_global</code>. But now I wish to create a new module, import <code>B</code> to it, and have <code>B</code> use <code>my_global</code> from the module it's been imported into, instead of the <code>my_global</code> from the module <code>B</code> was original defined.</p> <pre><code># module_b.py from module_a import B my_global = [] </code></pre> <h2>Related</h2> <p>I'm struggling to explain my problem, here is my previous attempt which did in fact ask something completely different:</p> <ul> <li><a href="https://stackoverflow.com/q/7483022/149482">Clone a module and make changes to the copy</a></li> </ul> <h2>Update0</h2> <ul> <li>The example above is only for illustration of what I'm trying to achieve.</li> <li>Since there is no variable scope for classes (unlike say, C++), I think a reference to a globals mapping is not stored in a class, but instead is attached to every function when defined.</li> </ul> <h2>Update1</h2> <p>An example was requested from the standard library:</p> <p>Many (maybe all?) of the classes in the <a href="http://hg.python.org/cpython/file/0408001e4765/Lib/threading.py" rel="nofollow noreferrer"><code>threading</code></a> module make use of globals such as <code>_allocate_lock</code>, <code>get_ident</code>, and <code>_active</code>, defined <a href="http://hg.python.org/cpython/file/0408001e4765/Lib/threading.py#l26" rel="nofollow noreferrer">here</a> and <a href="http://hg.python.org/cpython/file/0408001e4765/Lib/threading.py#l595" rel="nofollow noreferrer">here</a>. One cannot change these globals without changing it for all the classes in that module.</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.
 

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