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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:47:33+00:00 2026-06-17T23:47:33+00:00

Context: I’d like to be able to decorate functions so that I can track

  • 0

Context:

I’d like to be able to decorate functions so that I can track their stats. Using this post as a reference I went about trying to make my own callable decorator objects.

Here is what I ended up with:

def Stats(fn):
    Class StatsObject(object):
        def __init__(self, fn):
            self.fn = fn
            self.stats = {}

        def __call__(self, obj, *args, **kwargs):
            self.stats['times_called'] = self.stats.get('times_called', 0) + 1
            return self.fn(obj, *args, **kwargs)

    function = StatsObject(fn)
    def wrapper(self, *args **kwargs):
        return function(self, *args, **kwargs)
    return wrapper

Class MockClass(object):
    @Stats
    def mock_fn(self, *args, **kwargs):
        # do things

Problem:

This actually calls the mock_fn function correctly but I don’t have a reference to the stats object outside the wrapper function. i.e. I can’t do:

mc = MockClass()
mc.mock_fn()
mc.mock_fn.stats
# HasNoAttribute Exception

Then I tried changing the following code recognizing that it was a scoping issue:

From:

    function = StatsObject(fn)
    def wrapper(self, *args **kwargs):
        return function(self, *args, **kwargs)
    return wrapper

To:

    function = StatsObject(fn)
    return function

But of course I lost the self reference (self becomes the StatsObject instance, obj becomes the first arg, and the MockClass object self reference gets lost).

So I understand why the first issue is happening, but not the second. Is there any way that I can pass the self reference of MockClass to the StatsObject __call__ function?

  • 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-17T23:47:34+00:00Added an answer on June 17, 2026 at 11:47 pm

    Functions can actually themselves have attributes in Python.

    def Stats(fn):
        class StatsObject(object):
            def __init__(self, fn):
                self.fn = fn
                self.stats = {}
    
            def __call__(self, obj, *args, **kwargs):
                self.stats['times_called'] = self.stats.get('times_called', 0) + 1
                return self.fn(obj, *args, **kwargs)
    
        function = StatsObject(fn)
        def wrapper(self, *args **kwargs):
            return function(self, *args, **kwargs)
    
        # KEY LINE BELOW: make the StatsObject available outside as "stats_fn"
        wrapper.stats_fn = function
    
        return wrapper
    
    class MockClass(object):
        @Stats
        def mock_fn(self, *args, **kwargs):
            # do things
    

    The key line is assigning the StatsObject instance (which you’ve, perhaps misleadingly, locally named function) as an attribute of the function which you return from the decorator.

    Once you do this, self.mock_fn.stats_fn.stats (not self.mock_fn()! The attribute is on the function, not its return value) will work within an instance of MockClass, and MockClass.mock_fn.stats_fn.stats will be available outside. The statistics will be global across all instances of MockClass (since the decorator is called once, not once per instance), which may or may not be what you want.

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

Sidebar

Related Questions

Context Using Ruby I am parsing strings looking like this: A type with an
Context We have this application, using about 60 coding projects. We have several products
Context Sometimes, I like reading code on paper rather than on screen (so I
Context I'm currently reading about Clojure's implementation of monads: org.clojure/algo.monads Intuitively, reduce looks like
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
Context: Creating a custom MessageBox with text input How can I pass a variable
Context We have developed the application from end-to-end using .NET Stack. So, let's say
Context: I am playing music through a media element, and using a slider to
Context: I am using EF 5 and trying to bind two entities to a
Context I want to make this call via reflection instanceOfEventPublisher.Publish<T>(T eventInst); When I 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.