Naked function call syntax

Patrick K. O'Brien pobrien at orbtech.com
Sun Jul 15 17:36:03 EDT 2001


Here is a slightly better version, in that it handles parameters:

class PseudoKeyword:
    '''A callable class that calls a method passed as a parameter.

    Good for creating a pseudo keyword in the python runtime
    environment. The keyword is really an object that has a repr()
    that calls itself which calls the method that was passed in the
    init of the object. All this just to avoid having to type in the
    closing parens on a method. So, for example:

        >>> quit = PseudoKeyword(SomeObject.someMethod)
        >>> quit

    SomeObject.someMethod gets executed as if it had been called
    directly and the user didn't have to type the parens, like
    "quit()". This technique is most applicable for pseudo keywords
    like quit, exit and help. '''

    def __init__(self, method):
        # XXX Should probably check that this is a callable object.
        self.method = method

    def __call__(self, *args, **kwds):
        self.method(*args, **kwds)

    def __repr__(self):
        self()
        return ''

--
Patrick K. O'Brien
Orbtech
"I am, therefore I think."






More information about the Python-list mailing list