[OT] New to Python: Features

Alex Martelli aleaxit at yahoo.com
Thu Oct 7 03:52:02 EDT 2004


Eugeni Doljenko <dolzenko at rsu.ru> wrote:

> >>You're kidding, right. Right ? Without brackets ? That's... that's just
> >>not possible is it ?
> > In certain languages it is.  I think Lua lets you do it but more certainly
> > I know Ruby does.
> I'd love language that will let me call functions with square brackets.
> square brackets are so sweet.

hmmm, closest I can come is...:

class MakeEugeniHappy(object):
    def __init__(self, f):
        self.f = f
    def __getitem__(self, args):
        if isinstance(args, tuple): return self.f(*args)
        else: return self.f(args)

@ MakeEugeniHappy
def f(x): return x*3+1

Now, 'print f[23]' emits 70.  No way to call functions without
arguments, and a bit of a mess to call one with an argument that's a
tuple (I decided f[1,2,3] had better map to the original f(1,2,3) -- so
to call the original f((1,2,3)) you need f[(1,2,3),] ...!-).  One way
around the "call without arguments" would be to check if
isinstance(args, slice) and specialcase slices with None for all
attributes, so f[:] would call the original f().

> seriously, Ruby is syntactical madness. 

Nah, it ain't too bad, even though the call-w/o-parentheses part is
indeed a substantial minus.


Alex



More information about the Python-list mailing list