the name of a method

T

thomas.steffen75

Hello,

I have a Class:

class myClass:
def __init__(self):
# do something
print "name of class = " + self.__class__.__name__

def myMethod(self):
# do something
print "name of method = " + "myMethod"
return

...

I print the name of the class with self.__class__.__name__ in
__init__.
I want to print also in every method of myClass the name of the
method.
How can I get the name? I would not like to write e.g. "myMethod". Is
there a variable like self.__class__.__name__ for this?
Thanks for your hints, Thomas
 
D

Diez B. Roggisch

Hello,

I have a Class:

class myClass:
def __init__(self):
# do something
print "name of class = " + self.__class__.__name__

def myMethod(self):
# do something
print "name of method = " + "myMethod"
return

...

I print the name of the class with self.__class__.__name__ in
__init__.
I want to print also in every method of myClass the name of the
method.
How can I get the name? I would not like to write e.g. "myMethod". Is
there a variable like self.__class__.__name__ for this?
Thanks for your hints, Thomas

This can be done by inspecting the stackframes. Look into the module
inspect. This has also been discussed very often on this list, stackframe &
inspect should be good searchterms.

However, if what you are after is logging, you should take a look into the
logging module. it has many advantages over simple print-statements, and
amongst other things allows you to print out the enclosing callable name
when invoked ala

logger.debug("Some message.")

I strongly recommend using that. And at least you can of course peek into
the logging module's source to see how the extract that information.

Diez
 
J

Jason Scheirer

This can be done by inspecting the stackframes. Look into the module
inspect. This has also been discussed very often on this list, stackframe &
inspect should be good searchterms.

However, if what you are after is logging, you should take a look into the
logging module. it has many advantages over simple print-statements, and
amongst other things allows you to print out the enclosing callable name
when invoked ala

logger.debug("Some message.")

I strongly recommend using that. And at least you can of course peek into
the logging module's source to see how the extract that information.

Diez

I agree, this should not be done. However, sometimes it's useful to
see the parameter values:

import inspect
import logging
import sys
def log_fn():
logging.debug("%s%s" % (
sys._getframe().f_back.f_code.co_name,
inspect.formatargvalues(*inspect.getargvalues(sys._getframe
().f_back))))

logging.getLogger().setLevel(0)
def hello_there(x, y, z, *a):
log_fn()
return 1+x
DEBUG:root:hello_there(x=1, y='a', z='b', *a=(5, 6, 7, 8, 9))

It can be done, but usually you want to actually trace through with
the debugger.
 
M

Matimus

Hello,

I have a Class:

class myClass:
    def __init__(self):
        # do something
        print "name of class = " +  self.__class__.__name__

    def myMethod(self):
        # do something
        print "name of method = " + "myMethod"
        return

    ...

I print the name of the class with self.__class__.__name__ in
__init__.
I want to print also in every method of myClass the name of the
method.
How can I get the name? I would not like to write e.g. "myMethod". Is
there a variable like self.__class__.__name__ for this?
Thanks for your hints, Thomas

I would just use a decorator:
.... def new_meth(*args, **kwargs):
.... print meth.func_name
.... return meth(*args, **kwargs)
.... return new_meth
........ @print_method_name
.... def my_method(self):
.... pass
....my_method


Matt
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top