[Tutor] Accessing the name of a Function
Carroll, Barry
Barry.Carroll at psc.com
Wed Dec 13 02:02:56 CET 2006
Andreas:
You're right, that is kind of messy and somewhat limited. In the
present case, however, it is the function's defined name that I want, so
this would work okay.
I'm guessing that there is a way to determine the number and names of
the arguments to the function as well. I'll go look at the sys module
and see what I can find.
Regards,
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
> -----Original Message-----
> From: Andreas Kostyrka [mailto:andreas at kostyrka.org]
> Sent: Tuesday, December 12, 2006 4:05 PM
> To: Carroll, Barry
> Cc: tutor at python.org
> Subject: Re: [Tutor] Accessing the name of a Function
>
> * Carroll, Barry <Barry.Carroll at psc.com> [061212 23:54]:
> > Greetings:
> >
> >
> >
> > Is it possible, from inside a stand-alone function (not a member
of a
> > class), to access the string representation of the function's
name?
> If
> > so, how?
> Probably, but it's dirty like help (sys._getframe would be your ticket
> into this), but you ask on the tutor mailing list, so you'll probably
> don't want this:
>
> def func(x):
> return 2 * x
>
> gunc = func
> del func
>
> print gunc(10)
>
> The best you can hope to derive is "func" here, but as you can see,
> functions are only objects that can be assigned freely.
>
> (Spoiler: it's sys._getframe(0).f_code.co_name, but it will always
> only know the name the function was defined under. No way to know how
> it was named when it was called. So it's not worth much.)
>
> Andreas
> >
> >
> >
> > Regards,
> >
> >
> >
> > Barry
> >
> > [1]barry.carroll at psc.com
> >
> > 541-302-1107
> >
> > ________________________
> >
> > We who cut mere stones must always be envisioning cathedrals.
> >
> > --Quarry worker's creed
> >
> >
> >
> > References
> >
> > Visible links
> > 1. mailto:barry.carroll at psc.com
>
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list