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

  • Home
  • SEARCH
  • 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 7786287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:23:34+00:00 2026-06-01T20:23:34+00:00

I have class Window. This class has method onClick which gets argument the id

  • 0

I have class Window. This class has method onClick which gets argument the id of control clicked on that window (i can not change the way it’s work):

 class Window(SoneBaseWindowClass):

    def onClick(self,controlID):
        print controlID

I want to make decorator which will add my method to callback stack.
And than method onClick will call certain method when certain control get clicked.
So:

class Window(SoneBaseWindowClass):
    def __init__(self):
        callback_stack = {}

    def onClick(self,controlId):
        callback = self.callback_stack.get(controlID)
        if callback is not None:
            callback()

    # suppose to add decorated method to a callback_stack
    def onclick_decorator(method):
        pass

So I need to make decorator which will add some method to callback_stack. Example using.

class MyWindow(Window):

    @onclick_decorator(12345)
    def someMethod(self):
        print "Gets called when control with id 12345 clicked"

Actually the question is how can i get acces to class from onclick_decorator
Self is not passed to that method. So i can assume that it could get acces to class, but could not figure out how.

  • 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-01T20:23:35+00:00Added an answer on June 1, 2026 at 8:23 pm

    The way to deal with this kind of situation is to have the decorator to add the decorated methods to a data structure (CallbackStack in your case) – but to attach that data structure to the class, you need some action when the class is created (i.e. parsed) – since while it is being parsed, the decorator, as you say, don’t have access to either the instance or class.

    In modern Python (even Python 2) there are two ways to do that: you eitehr use a metclass, or a class decorator. But I don think class decorators are available for Python 2.4 – so you will need to have a metaclass.

    It is not that complicated – the __new__method on the metaclass gets passd the dictionary that was constructed during parsing. All your decorator has to do is to mark the desired methods with an attribute that the metaclass can find – and build your callback_stack from these markings. The call_back stack can even be a dictionary:

    def on_click_decorator(key):
        def real_decorator(func):
            setattr(func, "_callback_to", key)
            return func
        return real_decorator
    
    
    class Meta(type):
        def __new__(metacls, name, bases, dct):
            callback_stack = {}
            for name, value in dct.items():
                if hasattr(value, "_callback_to"):
                    callback_stack[value._callback_to] = key
            dct["callback_stack"] = callback_stack
            # we've done all needed manipulation - no
            # need for the class to be from a special type - so we
            # don't call type.__new__
            return type(name, bases, dct)
    

    Now you can, from inside your methods, call
    self.callback_stack[controlID] to retrieve the registered functions
    (ou get them as functions not methods with the code above, so pass
    “self” explicitly to them).

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

Sidebar

Related Questions

In a C++ application, let's say I have a window class, which has several
We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window
I have this code: public class Window extends JFrame { public Window(){ ... JButton
I have this class which I want to pass around Windows as LPARAM parameter.
Imagine I have a class Window with a member function show which causes the
I'm trying to hide window after its startup. I have own window-class which is
I have a basic Window class that I need to have events such as
The Application class in System.Windows.Forms have a some properties that can be quite useful.
lets say I have an NSWindow Class, that has several events for mouse and
QtCreator has this nice feature to bring up the API-doc for a class/method when

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.