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 1009033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:51:49+00:00 2026-05-16T08:51:49+00:00

[Updated]: Answer inline below question I have an inspecting program and one objective is

  • 0

[Updated]: Answer inline below question

I have an inspecting program and one objective is for logic in a decorator to know whether the function it is decorating is a class method or regular function. This is failing in a strange way. Below is code run in Python 2.6:

def decorate(f):
    print 'decorator thinks function is', f
    return f

class Test(object):
    @decorate
    def test_call(self):
        pass

if __name__ == '__main__':
    Test().test_call()
    print 'main thinks function is', Test().test_call

Then on execution:

decorator thinks function is <function test_call at 0x10041cd70>
main thinks function is <bound method Test.test_call of <__main__.Test object at 0x100425a90>>

Any clue on what’s going wrong, and if it is possible for @decorate to correctly infer that test_call is a method?

[Answer]
carl’s answer below is nearly perfect. I had a problem when using the decorator on a method that subclasses call. I adapted his code to include a im_func comparison on superclass members:

ismethod = False
for item in inspect.getmro(type(args[0])):
    for x in inspect.getmembers(item):
        if 'im_func' in dir(x[1]):
            ismethod = x[1].im_func == newf
            if ismethod:
                break
    else:
        continue
    break
  • 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-05-16T08:51:49+00:00Added an answer on May 16, 2026 at 8:51 am

    As others have said, a function is decorated before it is bound, so you cannot directly determine whether it’s a ‘method’ or ‘function’.

    A reasonable way to determine if a function is a method or not is to check whether ‘self’ is the first parameter. While not foolproof, most Python code adheres to this convention:

    import inspect
    ismethod = inspect.getargspec(method).args[0] == 'self'
    

    Here’s a convoluted way that seems to automatically figure out whether the method is a bound or not. Works for a few simple cases on CPython 2.6, but no promises. It decides a function is a method if the first argument to is an object with the decorated function bound to it.

    import inspect
    
    def decorate(f):
        def detect(*args, **kwargs):
            try:
                members = inspect.getmembers(args[0])
                members = (x[1].im_func for x in members if 'im_func' in dir(x[1]))
                ismethod = detect in members
            except:
                ismethod = False
            print ismethod
    
            return f(*args, **kwargs)
        return detect
    
    @decorate
    def foo():
        pass
    
    class bar(object):
        @decorate
        def baz(self):
            pass
    
    foo() # prints False
    bar().baz() # prints True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling I may already know the answer to this question, but
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
I have been following the answer of this question: How to update existing object
**Updated: (See below)**I have been looking around for couple of days and can't find
I have a question concerning volatile keyword I can't seem to find an answer
UPDATED ANSWER TO QUESTION: How do I get a lookup value (from Atc) to
Updated 10/21: Changed title and question in order to possibly get an answer (other
NOTE: I added my new solution at the UPDATE answer below. I try to
Platform: ASP.NET 4.0, MVC 4 RC, VS 2012 Update: I've answer my question, myself.
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from

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.