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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:12:13+00:00 2026-05-26T00:12:13+00:00

Note : The accepted answer on the other question shows how to use the

  • 0

Note:

The accepted answer on the other question shows how to use the parent decorater.

The accepted answer on this question shows moving the decorator to the module scope.


EDIT: Using the previous example was a bad idea. Hopefully this is more clear:

class A:
    def deco( func ):
        print repr(func)
        def wrapper( self, *args ):
            val = func( *args )
            self.do_something()
            return val
        return wrapper

    def do_something( self ):
        # Do something
        print 'A: Doing something generic for decoration'

    @deco
    def do_some_A_thing ( self ):
        # Do something 
        print 'A: Doing something generic'

class B ( A ):

    @deco
    def do_some_B_thing( self ):
        # Do something
        print "B: Doing something specific"

a = A()
b = B()
a.do_some_A_thing()
b.do_some_B_thing()

#Expected Output:
    #A: Doing something generic
    #A: Doing something generic for decoration
    #B: Doing something specific
    #A: Doing something generic for decoration

This code generates a NameError: name ‘deco’ is not defined inside B.
The decorator needs to be inside the class scope because I require access to stored state.

Third Edit: On Sven’s suggestions, I tried this:

class A:
    def deco( func ):
        def wrapper( self, *args ):
            val = func( *args )
            self.do_something(*args)
            return val
        return wrapper

    def do_something( self ):
        # Do something
        print 'A: Doing something generic for decoration'

    @deco
    def do_some_A_thing ( self ):
        # Do something 
        print 'A: Doing something generic'

    deco = staticmethod(deco)

class B ( A ):

    @A.deco
    def do_some_B_thing( self ):
        # Do something
        print "B: Doing something specific"



a = A()
b = B()
a.do_some_A_thing()
b.do_some_B_thing()

#Expected Output:
    #A: Doing something generic
    #A: Doing something generic for decoration
    #B: Doing something specific
    #A: Doing something generic for decoration

I now have have TypeError: do_some_A_thing() takes exactly 1 argument (0 given). Any pointers?

  • 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-26T00:12:13+00:00Added an answer on May 26, 2026 at 12:12 am

    The problem is that inheritance works for instance attribute lookup, not for class definitions. So when you try to decorate with A.deco in B, it can’t find it. The solution is to move deco out to module scope, and because there is nothing magic about the name self, you can keep using it. You also need to explicitly pass self to func, and you do not need to pass it in self.do_something(). Here’s the updated code:

    def deco( func ):
        print repr( func )
        def wrapper( self, *args ):
            val = func( self, *args )
            self.do_something()
            return val
        return wrapper
    
    class A:
        def do_something( self ):
            # Do something
            print 'A: Doing something generic for decoration'
    
        @deco
        def do_some_A_thing ( self ):
            # Do something 
            print 'A: Doing something generic'
    
    class B ( A ):
    
        @deco
        def do_some_B_thing( self ):
            # Do something
            print "B: Doing something specific"
    
    a = A()
    b = B()
    a.do_some_A_thing()
    b.do_some_B_thing()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note This is not a REBOL-specific question. You can answer it in any language.
While looking at some questions on optimization, this accepted answer for the question on
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note : The code in this question is part of deSleeper if you want
Quick note on the accepted answer : I disagree with a small part of
Note: This was posted when I was starting out C#. With 2014 knowledge, I
Note The question below was asked in 2008 about some code from 2003. As
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
I need to get execution time in milliseconds. Note : I originally asked this

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.