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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:43:06+00:00 2026-05-17T06:43:06+00:00

Example code: # -*- coding: utf-8 -*- from functools import wraps class MyClass(object): def

  • 0

Example code:

# -*- coding: utf-8 -*-
from functools import wraps

class MyClass(object):
  def __init__(self):
    pass

  #decorator inside class
  def call(f):
    @wraps(f)
    def wrapper(*args):
      print 'Wrapper: ', args
    return wrapper

  #decorated 'method' without self
  @call
  def myfunc(a): 
    pass

c = MyClass()
c.myfunc(1)

Returns:

Wrapper:  (<test3.MyClass object at 0xb788a34c>, 1)

Is this normal? Can someone explain?

If this is a feature I would use it in my library.

  • 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-17T06:43:07+00:00Added an answer on May 17, 2026 at 6:43 am

    This is perfectly normal.

    The function myfunc is replacecd by an instance of wrapper. The signature of wrapper is (*args). because it is a bound method, the first argument is the instance of MyClass which is printed out after the string `Wrapper: ‘.

    What’s confusing you?

    It’s worth noting that if you use call as a decorator from outside of MyClass, it will generate a TypeError. One way around this is to apply the staticmethod decorator to it but then you can’t call it during class construction.

    It’s a little bit hacky but I address how to have it both ways here.

    update after comment

    it gets the instance as the first argument regardless of if you type self in the parameter list because after the class is created, and an instance instantiated, it is a bound method. when you call it in the form

    @instance.call
    def foo(bar):
        return bar + 1
    

    it expands to

    def foo(bar):
        return bar + 1
    foo = instance.call(f)
    

    but note that you are calling it on an instance! This will automatically expand to a call of the form

    def foo(bar):
        return bar + 1
    foo = MyClass.call(instance, f)
    

    This is how methods work. But you only defined call to take one argument so this raises a TypeError.

    As for calling it during class construction, it works fine. but the function that it returns gets passed an instance of MyClass when it is called for the same reason that I explained above. Specifically, whatever arguments you explicity pass to it come after the implicit and automatic placement of the instance that it is called upon at the front of the argument list.

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

Sidebar

Related Questions

I have the following example code: class A(object): def __init__(self, id): self.myid = id
here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
Is there good example code or a test project for explaining the Model–view–presenter (MVP)
Many C++ books contain example code like this... std::cout << Test line << std::endl;
There is this example code, but then it starts talking about millisecond / nanosecond
Does anyone have an example (code or a link) that will allow me to
I see a lot of example code for C# classes that does this: public
I've gone through most of the example code and I still need some help.
Just about every piece of example code everywhere omits error handling (because it confuses
Can someone give me some example code that creates a surface with a transparent

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.