command pattern/framework in python?

Darrell darrell at dorb.com
Fri Apr 28 20:22:43 EDT 2000


<tiddlerdeja at my-deja.com> wrote in message
> Anyone got an example of a Command pattern/framework that they have
> used in python?
>

This pattern wraps an action or command in an instance with an execute
method.
The instance can be passed around like a function pointer.

def demo(a,b,c):
    print 'a:',a
    print 'b:',b
    print 'c:',c

class Command:
    def __init__(self, cmd, *args):
        self._cmd=cmd
        self._args=args

    def __call__(self, *args):
       return apply(self._cmd, self._args+args)


cmd=Command(dir,__builtins__)
print cmd()

cmd=Command(demo,1,2)
cmd(3)

--Darrell Gallion





More information about the Python-list mailing list