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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:17:43+00:00 2026-06-07T03:17:43+00:00

In the past I’ve written python code with all functions in the same file,

  • 0

In the past I’ve written python code with all functions in the same file, and I could profile my programs using the following code:

This is a decorator I wrote:

def do_profile(cond):
    def resdec(f):
        if not cond:
            return f
        return profile(f)
    return resdec

And this is how I use it:

@do_profile(DO_PROFILE)
def my_func():
    return 1

I would then invoke kernprof.py on my script:

kernprof.py  -l my_program.py

In the meantime I got more familiar with OOP and I rewrote my program into many classes and the program is now started like this:

 if __name__ == "__main__":
     my_app = myApp()
     my_app.run()

myApp is a class which is also communicating heavily with other classes:

class myApp():
    @do_profile(DO_PROFILE)
    def foo_method(self, arg1):
        pass

I’ve added the do_profile decorator in front of each myApp method, but if I run kernprof.py, the resulting .prof file is empty

So what’s the easiest way of profiling a methods of a class? I would really love to switch this on / off with a decorator and a flag.

EDIT1: I’m really interested in the easiest possible solution here. A find a decorator to be an elegant solution, but maybe things can be done easier. What I DO NOT want to do, is using stuff like cProfile's profile profile.runctx('self.baz()', globals(), locals()) . That is not a practical solution when handling many classes and methods.

  • 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-07T03:17:45+00:00Added an answer on June 7, 2026 at 3:17 am

    The profile function is a decorator itself, and like most decorators, they need to be applied to functions only.

    Luckily, class methods are basically functions that are bound to an instance when an instance is created. Thus, you can apply your decorator to any class method by putting it in the class definition by the methods themselves:

    class myApp(object):
        @do_profile(DO_PROFILE)
        def foo_method(self, arg1):
            pass
    
        @do_profile(DO_PROFILE)
        def bar_method(self, arg2):
            pass
    

    If you use python 2.6 or up, you can also create a class decorator and apply the profile decorator to all methods on any given class. You’d apply it by placing the decorator right before the class definition:

    @do_profile_all_methods(DO_PROFILE)
    class myApp(object):
        def foo_method(self):
            pass
    

    Such a decorator could look something like this:

    import types
    
    def do_profile_all_methods(cond):
        if not cond:
            return lambda c: c # Do nothing with the class; the 'null' decorator
        def profile_all_methods(klass):
            for name, attr in klass.__dict__.items():
                if isinstance(attr, types.UnboundMethodType):
                    klass[name] = profile(attr)
            return klass
        return profile_all_methods
    

    This decorator only applies the profile wrapper to direct methods, not any inherited from the base class.

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

Sidebar

Related Questions

In past, I am using Listview and using below code can show a particular
This past week I was given an SQL lab involving functions. My knowledge of
How do I get past this variable initialization problem? If I only could figure
I read past questions but I couldn't solve my issue :( This is my
In the past few years of using svn, I've frequently run into problems where
In past i have worked with JQuery 1.2 vsdoc file which generates intellisense for
From past few days I'm trying to develop a regex that fetch all the
In past versions of django you could construct a queryset and then do .as_sql()
These past few days I've been working toward converting my PHP code base from
For past 7 months I have been designing web applications using open source technologies

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.