What's the use of changing func_name?

hemanth hemanthps at gmail.com
Thu May 19 02:39:17 EDT 2005


could ildg wrote:
> Thank you for your help.
> I know the function g is changed after setting the func_name.
> But I still can't call funciton g by using f(), when I try to do
> this, error will occur:
> <code>
> >>> g.func_name="f"
> >>> print g
> <function f at 0x00B2CEB0>
> >>> f()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> NameError: name 'f' is not defined
> </code>
> Since the name of g is changed into f, why can't I call it by using
f()?
> Should I call it using f through other ways? Please tell me. Thanks~

Merely changing func_name to 'f' will not register 'f' in the module's
namespace. So calling f() will cause a NameError as there is no
function object bound to the name 'f' in the namespace. Instead,
if you say
f = g
then 'f' is bound in the namespace to the function object pointed to by
'g' and you can now make the call f().




More information about the Python-list mailing list