<div class="gmail_quote">Hi Ian,<br><br>On Tue, Sep 7, 2010 at 20:00, Ian <span dir="ltr"><<a href="mailto:hobson42@gmaiil.com">hobson42@gmaiil.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000">
<div class="im">
On 07/09/2010 11:50, Bruno Desthuilliers wrote:
<br>
</div><div class="im"><blockquote type="cite"><blockquote type="cite">note the order of the above - log is
defined before the import.
<br>
</blockquote>
And ? Do you think it will affect the imported module in any way ?
Like, say, magically "inject" your log function in the
DelNotePrinter module ?-)
<br>
</blockquote></div>
Just that log is defined before the global log is encountered to
that if the compiler needed to set up a link at compile time it was
able to do so. <br><div class="im">
<br></div></div></blockquote><div><br>I think I see where you're going wrong -- this bit me too when I was learning Python, having come from PHP. Unlike PHP, when you import a module in Python it does *not* inherit the importing module's namespace. So the "log" function you're accessing in DelNotePrinter.py is not the one you defined above the import statement.<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div bgcolor="#ffffff" text="#000000"><div class="im"><blockquote type="cite"><br>
In Python, "global" means "module-level", and it's only necessary
when you want to rebind a module-level name from within a function
or method.
<br>
</blockquote></div>
Exactly! I want to bind the name log to the function I wrote. <br></div></blockquote><div><br>As Bruno pointed out, the "global" statement is only necessary if you want to "rebind" a name -- i.e. if you want to modify how an object appears to the global scope. It's not necessary if you simply want to call a function.<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div bgcolor="#ffffff" text="#000000"><div class="im">
<br>
<blockquote type="cite">
<blockquote type="cite">I have read
<a href="http://docs.python.org/reference/simple_stmts.html#global" target="_blank">http://docs.python.org/reference/simple_stmts.html#global</a> very
carefully and I still don't understand.
<br>
</blockquote>
<br>
The statement definition makes no sense if you don't understand
namespaces and bindings:
<br>
<br>
<a href="http://docs.python.org/reference/executionmodel.html#naming-and-binding" target="_blank">http://docs.python.org/reference/executionmodel.html#naming-and-binding</a>
<br>
<br>
</blockquote></div>
Thanks for the pointer. What a truly execrable piece of writing -
full of over-long sentences and multiple subordinate clauses. It
routinely uses terms before definition, and sometimes without
definition. It is astonishingly bad. <br></div></blockquote><div><br>Perhaps you could help rewrite it? I'm sure the maintainers would be very happy to get a documentation patch.<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000">
<br>
The third sentence reads "Each occurrence of a name in the program
text refers to the <i>binding</i> of
that name established in the innermost function block containing the
use." What does that mean? It appears to mean that it is the
assignment in the innermost function block that binds for all uses,
not the first, not module blocks, not class blocks and not code
blocks - but the innermost function block. That might be correct,
but somehow I doubt it, for it would mean that earlier bindings are
ignored or invalidated or not used or something - even if the inner
block is not executed. <br>
<br>
I am not stupid and English is my mother tongue, and I have read
that page many times. The implications of the passage are still
opaque to me. <br>
<br>
How can I call a global function placed at the top of the source.
Do I really have to move a 4 line function into its own file and
import it again and again and again? <br>
</div></blockquote><div><br clear="all"></div></div>The short answer is yes. If you have this and similar functions that you call from various modules, then it's ideal to put them in a utility module of their own. However, as imported modules are cached in Python, this isn't as expensive as you might think if you are more used to other languages.<br>
<br>-- <br>Rami Chowdhury<br>"Never assume malice when stupidity will suffice." -- Hanlon's Razor<br>408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)<br>