Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Why are you doing that?</strong> <em>(exec apart)</em><br> <strong>Why are you trying to do that with <code>exec</code>?</strong></p> <p>Also, doing it with <code>exec</code> will:</p> <ol> <li>Not work.</li> <li>Give different results in <code>python-2.x</code> and in <code>python-3.x</code>.</li> </ol> <p>Example:</p> <pre><code>class demo: a = 'a' print(demo.a) def start(): global demo class demo: b = "b" try: print(demo.a) except AttributeError: print(demo.b) if __name__ == '__main__': start() try: print(demo.a) except AttributeError: print(demo.b) </code></pre> <p>That either in <code>python-2.x</code> and in <code>python-3.x</code> will give:</p> <pre><code>a b b </code></pre> <p>And now let's try it with <code>exec</code>:</p> <pre><code>class demo: a = 'a' print(demo.a) def start(): exec('global demo', globals(), locals()) exec('class demo: b = "b"', globals(), locals()) try: print(demo.a) except AttributeError: print(demo.b) if __name__ == '__main__': start() try: print(demo.a) except AttributeError: print(demo.b) </code></pre> <p>Output <code>python2.7</code>:</p> <pre><code>a b a </code></pre> <p>Output <code>python3.2</code>:</p> <pre><code>a a a </code></pre> <hr> <p><strong>Q: How to 'dynamically create the class'?</strong></p> <p>As <em>kindall</em> already told you, <code>exec</code> is not the way to do that.</p> <p>A <a href="https://stackoverflow.com/q/100003/1132524">metaclass</a> or a <a href="https://stackoverflow.com/q/456672/1132524">class factory</a> does that, but are you sure you actually need that?</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. This table or related slice is empty.
    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