What's the use of changing func_name?

could ildg could.net at gmail.com
Thu May 19 02:16:48 EDT 2005


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~


On 5/19/05, Robert Kern <rkern at ucsd.edu> wrote:
> could ildg wrote:
> > def a(func):
> >     def _inner(*args, **kwds):
> >         print "decorating..."
> >         return func(*args, **kwds)
> >     _inner.func_name = func.func_name -->when I delete this line, the
> > rusult is the same. why?
> >     return _inner
> >
> > @a
> > def g(*args):
> >     for x in args:
> >         print x
> >     print "this is in function g"
> >
> > g(1,2,3,4,5)
> >
> > func_name is writable in python 2.4. I wonder what's the use of
> > writing it. Consider the code above, to change the func_name or not
> > will get the completely same result. The result is as
> > below:
> >
> > decorating...
> > 1
> > 2
> > 3
> > 4
> > 5
> > this is in function g
> >
> > Please tell me how does this happen, thank you~
> 
> Well, the function you posted certainly doesn't change the literal
> string you have inside when you change its func_name. However, the
> function object *does* change.
> 
> In [1]:def g(x):
>     ...:    pass
>     ...:
> 
> In [2]:g
> Out[2]:<function g at 0x5aedf0>
> 
> In [3]:g.func_name = 'f'
> 
> In [4]:g
> Out[4]:<function f at 0x5aedf0>
> 
> --
> Robert Kern
> rkern at ucsd.edu
> 
> "In the fields of hell where the grass grows high
>   Are the graves of dreams allowed to die."
>    -- Richard Harter
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list