How can I programmatically find the name of a method from within that method?

Discussion in 'Python' started by kj7ny, Aug 8, 2007.

  1. kj7ny

    kj7ny Guest

    Is there a way that I can programmatically find the name of a method I
    have created from within that method? I would like to be able to log
    a message from within that method (def) and I would like to include
    the name of the method from which it was written without having to
    hard-code that value in every message string. While we're at it, is
    there a way to programmatically get the name of the class and the
    module while I'm at it?

    Thanks,
     
    kj7ny, Aug 8, 2007
    #1
    1. Advertising

  2. kj7ny

    Jay Loden Guest

    Re: How can I programmatically find the name of a method from withinthat method?

    kj7ny wrote:
    > Is there a way that I can programmatically find the name of a method I
    > have created from within that method? I would like to be able to log
    > a message from within that method (def) and I would like to include
    > the name of the method from which it was written without having to
    > hard-code that value in every message string. While we're at it, is
    > there a way to programmatically get the name of the class and the
    > module while I'm at it?


    This is a frequently asked question around here :)

    You should search the list archives for past threads, e.g:
    http://aspn.activestate.com/ASPN/Mail/Message/python-list/3542665

    -Jay
     
    Jay Loden, Aug 8, 2007
    #2
    1. Advertising

  3. kj7ny

    kj7ny Guest

    On Aug 7, 10:09 pm, Jay Loden <> wrote:
    > kj7ny wrote:
    > > Is there a way that I can programmatically find the name of a method I
    > > have created from within that method? I would like to be able to log
    > > a message from within that method (def) and I would like to include
    > > the name of the method from which it was written without having to
    > > hard-code that value in every message string. While we're at it, is
    > > there a way to programmatically get the name of the class and the
    > > module while I'm at it?

    >
    > This is a frequently asked question around here :)
    >
    > You should search the list archives for past threads, e.g:http://aspn.activestate.com/ASPN/Mail/Message/python-list/3542665
    >
    > -Jay


    Thanks for the link. I had actually searched the past threads, but
    apparently didn't enter the right search criteria because I did not
    find that thread. Or, that thread isn't findable by searching Google
    groups?

    I tried the example in the interpreter and it appears to work.
    Despite my years and years of programming in python, I am a bit
    baffled by the example though. What is @checkPrivs (see example
    copied below from other post)? In fact... how does the thing work at
    all?

    ------------------------------------------
    def checkPrivs(fn):
    fnName = fn.func_name
    def restricted(*args):
    print "about to call function", fnName
    if fnName in listOfAllowedFunctions:
    return fn(*args)
    else:
    raise KeyError("you don't have sufficient privileges to do
    THAT")
    return restricted

    listOfAllowedFunctions = ['add','subtract']

    @checkPrivs
    def add(a,b):
    return a+b

    @checkPrivs
    def subtract(a,b):
    return a-b

    @checkPrivs
    def multiply(a,b):
    return a*b

    add(1,2)
    subtract(4,1)
    multiply(3,2)
     
    kj7ny, Aug 8, 2007
    #3
  4. kj7ny

    Peter Otten Guest

    kj7ny wrote:

    > What is @checkPrivs (see example copied below from other post)? In
    > fact... how does the thing work at all?


    > @checkPrivs
    > def add(a,b):
    > return a+b


    @... is called a decorator and is just a fancy way of writing

    def add(a, b):
    return a+b
    add = checkPrivs(add)

    Peter
     
    Peter Otten, Aug 8, 2007
    #4
  5. kj7ny

    Tony Guest

    On Aug 8, 8:25 am, Peter Otten <> wrote:
    > kj7ny wrote:
    > > What is @checkPrivs (see example copied below from other post)? In
    > > fact... how does the thing work at all?
    > > @checkPrivs
    > > def add(a,b):
    > > return a+b

    >
    > @... is called a decorator and is just a fancy way of writing
    >
    > def add(a, b):
    > return a+b
    > add = checkPrivs(add)
    >
    > Peter


    Is this cheating?

    class a:


    def square(self, x):

    print 'executing:', dir(self)[-1]
    print x*x
    def cube(self, x):
    print 'executing:', dir(self)[-2]
    print x*x*x

    b=a()



    b.square(3)
    b.cube(3)
    Output:

    PyMate r6780 running Python 2.3.5 (python)
    >>> function self naming2.py


    executing: square
    9
    executing: cube
    27
     
    Tony, Aug 8, 2007
    #5
  6. kj7ny

    Peter Otten Guest

    Tony wrote:

    > Is this cheating?


    Isn't it harder to calculate the magic indices than just writing down the
    names twice?

    > class a:
    >         def square(self, x):
    >                 print 'executing:', dir(self)[-1]
    >                 print x*x
    >         def cube(self, x):
    >                 print 'executing:',     dir(self)[-2]
    >                 print x*x*x
    >
    > b=a()
    > b.square(3)
    > b.cube(3)
    > Output:
    >
    > PyMate r6780 running Python 2.3.5 (python)
    > >>> function self naming2.py

    >
    > executing: square
    > 9
    > executing: cube
    > 27


    > Is this cheating?


    No, just wrong.

    >> class A:

    .... def alpha(self): return dir(self)[-2]
    .... def gamma(self): return dir(self)[-1]
    ....
    >>> a = A()
    >>> a.alpha(), a.gamma()

    ('alpha', 'gamma')
    >>> a.beta = 42
    >>> a.alpha(), a.gamma()

    ('beta', 'gamma')

    Peter
     
    Peter Otten, Aug 8, 2007
    #6
  7. kj7ny

    Tony Guest

    On Aug 8, 9:28 pm, Peter Otten <> wrote:

    > No, just wrong.
    >
    > >> class A:

    >
    > ... def alpha(self): return dir(self)[-2]
    > ... def gamma(self): return dir(self)[-1]
    > ...>>> a = A()
    > >>> a.alpha(), a.gamma()

    > ('alpha', 'gamma')
    > >>> a.beta = 42
    > >>> a.alpha(), a.gamma()

    >
    > ('beta', 'gamma')
    >
    > Peter

    Only wrong if the function is only to write its own name. if it does
    something else as well, seems to work:

    class a:

    def square(self, x):
    print 'executing:', dir(self)[-1]
    print x*x
    def cube(self, x):
    print 'executing:', dir(self)[-2]
    print x*x*x

    b=a()

    b.cube(4),b.square(2)
    b.c =4
    b.cube(3), b.cube(2)



    executing: cube
    64
    executing: square
    4
    executing: cube
    27
    executing: cube
    8

    cheers
     
    Tony, Aug 9, 2007
    #7
  8. kj7ny

    faulkner Guest

    On Aug 8, 12:45 am, kj7ny <> wrote:
    > Is there a way that I can programmatically find the name of a method I
    > have created from within that method? I would like to be able to log
    > a message from within that method (def) and I would like to include
    > the name of the method from which it was written without having to
    > hard-code that value in every message string. While we're at it, is
    > there a way to programmatically get the name of the class and the
    > module while I'm at it?
    >
    > Thanks,


    def foo():
    print sys._getframe(0).f_code.co_name

    most of the darkest magic of python is in the frames returned by
    sys._getframe.
     
    faulkner, Aug 9, 2007
    #8
  9. kj7ny

    faulkner Guest

    On Aug 8, 10:43 pm, faulkner <> wrote:
    > On Aug 8, 12:45 am, kj7ny <> wrote:
    >
    > > Is there a way that I can programmatically find the name of a method I
    > > have created from within that method? I would like to be able to log
    > > a message from within that method (def) and I would like to include
    > > the name of the method from which it was written without having to
    > > hard-code that value in every message string. While we're at it, is
    > > there a way to programmatically get the name of the class and the
    > > module while I'm at it?

    >
    > > Thanks,

    >
    > def foo():
    > print sys._getframe(0).f_code.co_name
    >
    > most of the darkest magic of python is in the frames returned by
    > sys._getframe.


    sorry for the double-post. i forgot to answer the rest of the
    question.

    class a:
    def b(self, *a):
    print sys._getframe(0).f_code.co_name
    print self.__class__.__name__
    print getattr(self,
    sys._getframe(0).f_code.co_name).im_class.__name__
    print self.__class__.__module__

    def log(f):
    def newf(*a, **kw):
    if a and f.func_code.co_varnames[0] == 'self': print '%s.%s.%s
    %r %r' % (a[0].__class__.__module__, a[0].__class__.__name__,
    f.func_name, a, kw)
    else: print '%s.%s %r %r' % (f.func_globals['__name__'],
    f.func_name, a, kw)
    f(*a, **kw)
    newf.__name__ = f.__name__
    newf.__doc__ = f.__doc__
    return newf

    you can find more interesting attributes of frame and function objects
    using the builtin dir function.
     
    faulkner, Aug 9, 2007
    #9
  10. kj7ny

    Peter Otten Guest

    Tony wrote:

    > On Aug 8, 9:28 pm, Peter Otten <> wrote:
    >
    >> No, just wrong.
    >>
    >> >> class A:

    >>
    >> ... def alpha(self): return dir(self)[-2]
    >> ... def gamma(self): return dir(self)[-1]
    >> ...>>> a = A()
    >> >>> a.alpha(), a.gamma()

    >> ('alpha', 'gamma')
    >> >>> a.beta = 42
    >> >>> a.alpha(), a.gamma()

    >>
    >> ('beta', 'gamma')
    >>
    >> Peter


    > Only wrong if the function is only to write its own name. if it does
    > something else as well, seems to work:
    >
    > class a:
    >
    >         def square(self, x):
    >                 print 'executing:', dir(self)[-1]
    >                 print x*x
    >         def cube(self, x):
    >                 print 'executing:',     dir(self)[-2]
    >                 print x*x*x
    >
    > b=a()
    >
    > b.cube(4),b.square(2)
    > b.c =4
    > b.cube(3), b.cube(2)


    You mean

    b.cube(3), b.square(2)

    > executing: cube
    > 64
    > executing: square
    > 4
    > executing: cube
    > 27
    > executing: cube
    > 8


    Yeah, cargo cult programming, I love it.

    dir() sorts attribute names alphabetically. Therefore the tail of the list
    you are accessing will only be altered if you choose a name >
    min(other_names), i. e. a name that comes after "cube" in the alphabet. Try
    setting

    b.root = 42

    if you don't believe me.

    Peter
     
    Peter Otten, Aug 9, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,904
    ding feng
    Jun 25, 2003
  2. 2pac
    Replies:
    20
    Views:
    1,147
    Keith Thompson
    Sep 10, 2003
  3. Mitko Haralanov
    Replies:
    17
    Views:
    437
    Ben Finney
    Oct 20, 2006
  4. Ezra Zygmuntowicz
    Replies:
    2
    Views:
    125
    Ezra Zygmuntowicz
    Jul 10, 2006
  5. Replies:
    6
    Views:
    211
Loading...

Share This Page