Hi Ian,

On Tue, Sep 7, 2010 at 20:00, Ian <hobso...@gmaiil.com> wrote:

>  On 07/09/2010 11:50, Bruno Desthuilliers wrote:
>
> note the order of the above - log is defined before the import.
>
> And ? Do you think it will affect the imported module in any way ? Like,
> say, magically "inject" your log function in the DelNotePrinter module ?-)
>
> 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.
>
>
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.


> 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.
>
> Exactly! I want to bind the name log to the function I wrote.
>

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.


>
>  I have read http://docs.python.org/reference/simple_stmts.html#globalvery 
> carefully and I still don't understand.
>
>
> The statement definition makes no sense if you don't understand namespaces
> and bindings:
>
> http://docs.python.org/reference/executionmodel.html#naming-and-binding
>
>  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.
>

Perhaps you could help rewrite it? I'm sure the maintainers would be very
happy to get a documentation patch.


>
> The third sentence reads "Each occurrence of a name in the program text
> refers to the *binding* 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.
>
> 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.
>
> 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?
>

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.

-- 
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to