Any way for a function to refer to itself?

Emile van Sebille emile at fenx.com
Fri Feb 23 11:37:15 EST 2001


Well then, it would need to be modified for additional
namespaces.  You could create a function that retrieves the
traceback for the caller, uses linecache to determine the
callers indentation level, walk up to the enclosing
indentation level, (unless already global), examine globals
for that name, verify it includes the right target name, and
then you'd have it.

See you,

--

Emile van Sebille
emile at fenx.com
-------------------


"Joshua Marshall" <jmarshal at mathworks.com> wrote in message
news:975rqs$59o$1 at news.mathworks.com...
> Emile van Sebille <emile at fenx.com> wrote:
> > I'm not sure if it always plays nice, but you may try
> > starting with:
>
> >>>> def test():
> >  "test function description"
> >  import traceback
> >  me = eval( traceback.extract_stack()[-1][2])
> >  print "DocString: %s  functionName: %s " % (me.__doc__,
> > me.__name__)
>
>
> >>>> test()
> > DocString: test function description  functionName: test
>
> This doesn't do what was asked - it only works if test is
defined in
> the global namespace:
>
>   def f():
>       def test():
>           "test function description"
>           import traceback
>           me = eval( traceback.extract_stack()[-1][2])
>           print "DocString: %s  functionName: %s " %
(me.__doc__, me.__name__)
>
>       test()
>
>   f()
>
> Yields:
>
>   Traceback (innermost last):
>     File "fu.py", line 10, in ?
>       f()
>     File "fu.py", line 8, in f
>       test()
>     File "fu.py", line 5, in test
>       me = eval( traceback.extract_stack()[-1][2])
>     File "<string>", line 0, in ?
>   NameError: test





More information about the Python-list mailing list