Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, that was interesting enough for me to experiment a bit and I read through <a href="http://docs.python.org/reference/executionmodel.html" rel="nofollow noreferrer">http://docs.python.org/reference/executionmodel.html</a></p> <p>Then did some tinkering with your code here and there, this is what i could find:</p> <p>code:</p> <pre><code>import pprint def two(): from pprint import pprint print globals()['pprint'] pprint('Eggs') print globals()['pprint'] def main(): if 'pprint' in globals(): print 'pprint is in globals()' global pprint print globals()['pprint'] pprint.pprint('Spam') from pprint import pprint print globals()['pprint'] pprint('Eggs') def three(): print globals()['pprint'] pprint.pprint('Spam') if __name__ == '__main__': two() print('\n') three() print('\n') main() </code></pre> <p>output:</p> <pre><code>&lt;module 'pprint' from '/usr/lib/python2.5/pprint.pyc'&gt; 'Eggs' &lt;module 'pprint' from '/usr/lib/python2.5/pprint.pyc'&gt; &lt;module 'pprint' from '/usr/lib/python2.5/pprint.pyc'&gt; 'Spam' pprint is in globals() &lt;module 'pprint' from '/usr/lib/python2.5/pprint.pyc'&gt; 'Spam' &lt;function pprint at 0xb7d596f4&gt; 'Eggs' </code></pre> <p>In the method <code>two()</code> <code>from pprint import pprint</code> but does not override the name <code>pprint</code> in <code>globals</code>, since the <code>global</code> keyword is <strong>not</strong> used in the scope of <code>two()</code>.</p> <p>In method <code>three()</code> since there is no declaration of <code>pprint</code> name in local scope it defaults to the global name <code>pprint</code> which is a module</p> <p>Whereas in <code>main()</code>, at first the keyword <code>global</code> <strong>is used</strong> so all references to <code>pprint</code> in the scope of method <code>main()</code> will refer to the <code>global</code> name <code>pprint</code>. Which as we can see is a module at first and is overriden in the <code>global</code> <code>namespace</code> with a method as we do the <code>from pprint import pprint</code> </p> <p>Though this may not be answering the question as such, but nevertheless its some interesting fact I think.</p> <p>=====================</p> <p><strong>Edit</strong> Another interesting thing.</p> <p>If you have a module say:</p> <p><code>mod1</code></p> <pre><code>from datetime import datetime def foo(): print "bar" </code></pre> <p>and another method say:</p> <p><code>mod2</code></p> <pre><code>import datetime from mod1 import * if __name__ == '__main__': print datetime.datetime.now() </code></pre> <p>which at first sight is seemingly correct since you have imported the module <code>datetime</code> in <code>mod2</code>.</p> <p>now if you try to run mod2 as a script it will throw an error:</p> <pre><code>Traceback (most recent call last): File "mod2.py", line 5, in &lt;module&gt; print datetime.datetime.now() AttributeError: type object 'datetime.datetime' has no attribute 'datetime' </code></pre> <p>because the second import <code>from mod2 import *</code> has overriden the name <code>datetime</code> in the namespace, hence the first <code>import datetime</code> is not valid anymore. </p> <p>Moral: Thus the order of imports, the nature of imports (from x import *) and the awareness of imports within imported modules - <strong>matters</strong>.</p>
 

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