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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:51:43+00:00 2026-05-13T23:51:43+00:00

I want to write a decorator that acts differently depending on whether it is

  • 0

I want to write a decorator that acts differently depending on whether it is applied to a function or to a method.

def some_decorator(func):
    if the_magic_happens_here(func): # <---- Point of interest
        print 'Yay, found a method ^_^ (unbound jet)'
    else:
        print 'Meh, just an ordinary function :/'
    return func

class MyClass(object):
    @some_decorator
    def method(self):
        pass

@some_decorator
def function():
    pass

I tried inspect.ismethod(), inspect.ismethoddescriptor() and inspect.isfunction() but no luck. The problem is that a method actually is neither a bound nor an unbound method but an ordinary function as long as it is accessed from within the class body.

What I really want to do is to delay the actions of the decorator to the point the class is actually instantiated because I need the methods to be callable in their instance scope. For this, I want to mark methods with an attribute and later search for these attributes when the .__new__() method of MyClass is called. The classes for which this decorator should work are required to inherit from a class that is under my control. You can use that fact for your solution.

In the case of a normal function the delay is not necessary and the decorator should take action immediately. That is why I wand to differentiate these two cases.

  • 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-13T23:51:43+00:00Added an answer on May 13, 2026 at 11:51 pm

    I would rely on the convention that functions that will become methods have a first argument named self, and other functions don’t. Fragile, but then, there’s no really solid way.

    So (pseudocode as I have comments in lieu of what you want to do in either case…):

    import inspect
    import functools
    
    def decorator(f):
      args = inspect.getargspec(f)
      if args and args[0] == 'self':
         # looks like a (future) method...
      else:
         # looks like a "real" function
         @functools.wraps(f)
         def wrapper  # etc etc
    

    One way to make it a bit more solid, as you say all classes involved inherit from a class under your control, is to have that class provide a metaclass (which will also of course be inherited by said classes) which checks things at the end of the class body. Make the wrapped function accessible e.g. by wrapper._f = f and the metaclass’s __init__ can check that all wrapped methods did indeed have self as the first argument.

    Unfortunately there’s no easy way to check that other functions (non-future-methods) being wrapped didn’t have such a first argument, since you’re not in control of the environment in that case. The decorator might check for “top-level” functions (ones whose def is a top-level statement in their module), via the f_globals (globals dict, i.e., module’s dict) and f_name attributes of the function — if the function’s such a global presumably it won’t later be assigned as an attribute of the class (thereby becoming a future-method anyway;-) so the self named first arg, if there, can be diagnosed as wrong and warned about (while still treating the function as a real function;-).

    One alternative would be to do the decoration in the decorator itself under the hypothesis of a real function, but also make available the original function object as wrapper._f. Then, the metaclass’s __init__ can re-do the decoration for all functions in the class body that it sees have been marked this way. This approach is much more solid than the convention-relying one I just sketched, even with the extra checks. Still, something like

    class Foo(Bar): ... # no decorations
    
    @decorator
    def f(*a, **k): ...
    
    Foo.f = f   # "a killer"... function becomes method!
    

    would still be problematic — you could try intercepting this with a __setattr__ in your metaclass (but then other assignments to class attributes after the class statement can become problematic).

    The more the user’s code has freedom to do funky things (and Python generally leaves the programmer a lot of such freedom), the harder time your “framework-y” code has keeping things under tight control instead, of course;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a python function decorator that tests that certain arguments to
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write something that acts just like confirm() in javascript, but I
i want to write a decorator that enables methods of classes to become visible
I want to write a command that specifies the word under the cursor in
I want to write a little DBQuery function in perl so I can have
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div
I'm trying to write a decorator that 'refreshes' after being called, but where the

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.