Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:58:07+00:00 2026-06-08T14:58:07+00:00

I have an awesome little function that looks like this: def verbose_print(message, *args, **kwargs):

  • 0

I have an awesome little function that looks like this:

def verbose_print(message, *args, **kwargs):
    """Prints `message` with a helpful prefix when in verbose mode

    Args:
        message (str): The message to print. Can be a format string, e.g.
            `A %s with some %s in it`
        *args: Variables for the message format
        **kwargs: Keyword variables for the message format
    """

    # Only print when in verbose mode
    if not config.verbose:
        return

    # Ready a prefix for the message
    try:
        s = inspect.stack()
        module_name = inspect.getmodule(s[1][0]).__name__
        func_name = s[1][3]
        prefix = '### %s->%s' % (module_name, func_name)
    except Exception as e:
        prefix = '### [stack unavailable]'

    if args:
        message = message % args
    elif kwargs:
        message = message % kwargs

    print '%s: %s' % (prefix, message)

The point of the function is that I can call it from anywhere with a message, and if my project config file is set to verbose mode, all the messages will be printed with a helpful prefix to show where it was called. Here’s an example of some output:

### avesta.webserver->check_login: Checking login for client at 127.0.0.1
### avesta.webserver->check_login: Found credentials cookie with username: tomas, token: blablabla
### avesta.webserver->check_login: Login valid, refreshing session
### avesta.webserver->get_flash_memory: Fetched flash data: None
### avesta.webserver->get: Fetched data from empty path ('previous_values', 'name'), returning ''
### avesta.webserver->get: Fetched data from empty path ('previous_values', 'description'), returning ''
### avesta.webserver->get: Fetched data from empty path ('validation_errors', 'name'), returning ''

The format is “### module->function: message”.

Now most of the time this is really helpful, but it’s not perfect. In the example above, the “get” function is actually a bound method of a class, but that’s not visible. What I’m trying to accomplish is that when a function is a bound method, I print with this format instead:

“### module->ClassName.function”

But the problem is:

  1. I only get the function name from the stack, so I can’t really check if it’s a bound method
  2. Even if I had the function reference, how would I extrapolate the class name it’s bound to?

Thanks for any answers that can help me figure this out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:58:08+00:00Added an answer on June 8, 2026 at 2:58 pm

    I thought this was going to be easy, but it turned out to be a bit complicated. If you have a reference to the bound method, you can get its class name via boundMethod.im_class.__name__. However, when you’re grabbing the stack, you can’t easily get a reference to the bound method, just to stack frames.

    However all is not lost! The inspect module can get you function arguments from a stack frame using the getargvalues function. You do have to cheat a little, by relying on the convention that methods always have their first argument named “self”. You can check for that, then grab the “self” value from the function’s locals dict, and from there it’s easy to get the class name. Try replacing your current try block with his code:

    s = inspect.stack()
    module_name = inspect.getmodule(s[1][0]).__name__
    func_name = s[1][3]
    arginfo = inspect.getargvalues(s[1][0])
    if len(arginfo.args) > 0 and arginfo.args[0] == "self":
        func_name = "%s.%s" (arginfo.locals["self"].__class__.__name__, func_name)
    prefix = '### %s->%s' % (module_name, func_name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this awesome MySQL query that's returning me an awesome array >
I have an awesome trouble with Gem. After executing this command: rm -f /usr/local/lib/ruby/gems/1.9.1/cache/*
What are my options for printing in Silverlight 3? Assume I have this awesome
I'm using CodeIgniter (because it's awesome) and I have something like: <?php echo anchor(/,
Just stumbled upon propertyGrid and its awesome! However, i have one task that i
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Since the advent of AS3 I have been working like this: private var loggy:String;
I have recently downloaded the most recent build of this awesome tool WSDL2OBJC from
I have an Arduino Uno (awesome little device!). It has two interrupts; let's call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.