ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: z_delegate.py
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/07/05
Version no: 1.0
Category: Extending

 

4 stars 1 vote(s)


Description:

This was written to emulate a flexible version of delegates from the C# language. Default arguments can be set and changed, and default arguments can be totally be replaced by new arguments on-the-fly. The delegate class was primarily written for working with Tkinter.

Source: Text Source

'''Support module for GUI programming.

This module provides access to the Delegate class
that was inspired by the C# programming language.'''

__version__ = 1.1

################################################################################

class Delegate:

    'Delegate(target, *args, **kwargs) -> new delegate'
    
    def __init__(self, target, *args, **kwargs):
        'x.__init__(...) initializes x'
        self.__target = target
        self.__args = args
        self.__kwargs = kwargs
        
    def __call__(self, *args, **kwargs):
        'x.__call__(*args, **kwargs) <==> x(*args, **kwargs)'
        if args or kwargs:
            return self.__target(*args, **kwargs)
        else:
            return self.__target(*self.__args, **self.__kwargs)
        
    def args(self, *args):
        'Sets args called on target.'
        self.__args = args
        return self
        
    def kwargs(self, **kwargs):
        'Sets kwargs called on target.'
        self.__kwargs = kwargs
        return self

################################################################################

if __name__ == '__main__':
    import sys
    print 'Content-Type: text/plain'
    print
    print file(sys.argv[0]).read()

Discussion:

When try to design a GUI with Tkinter, functions sometimes need to be dynamically bound to events with some of the parameters already filled in. Python 2.5 appears to possibly being doing something to solve this problem in with some helpful, new solutions; but until then, delegates may be what some people are currently looking for. The Delegate class basically forms a wrapper around a function and automatically changes how it operates based on whether or not arguments were passed to the wrapper when called. Default arguments and keyword arguments can easily be changed once the Delegate object is created.



Add comment

Number of comments: 1

Luke Plant, 2006/07/08
What's wrong with closures, or partial function application? (The function 'partial' doesn't exist in 2.4, but it's not hard to write.)
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.