Help with Python Grammar change

P.J.W.S. Vrijlandt P.J.W.S.VRIJLANDT at INT.azg.nl
Tue Feb 15 02:56:29 EST 2000


If you define:

def func(*arg):
    print 'func called with ', arg

class callable:

    def __init__(self, func, *arg):
        self.func = func
        self.arg = arg

    def __call__(self, *arg):
        apply(self.func, self.arg + arg)

class slicer:

    def __init__(self, func):
        self.func = func

    def __getitem__(self, arg):
        return callable(func, arg)

    def __getslice__(self, arg1, arg2):
        return callable(func, arg1, arg2)

g = slicer(func)

You can do:

>>> g[1:10](3)
func called with  (1, 10, 3)
>>> g[1:10:2](3)
func called with  (slice(1, 10, 2), 3)

No change in syntax needed!



> 
> I have a question for people familiar with the Python Grammar:
> 
> How difficult would it be and what are the problems with altering the
> grammar (and compile.c code) so that one can enter slice-syntax as an
> argument to a function call:
> 
> In other words if g is a function:
> 
> def g(a,b):
>     # some nifty code
>     return
> 
> I want
> 
> >>> g(1:10:2,3) 
> 
> to call function g with slice(1,10,2) and 3 as arguments.
> 
> Is this at all possible, or would it break things horribly?
> 
> Thanks for any feedback,
> 
> Travis Oliphant
> 
> 
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 
Met vriendelijke groet,

Patrick Vrijlandt




More information about the Python-list mailing list