functions without parentheses

Bengt Richter bokr at oz.net
Fri Jul 29 02:37:52 EDT 2005


On Thu, 28 Jul 2005 00:59:51 -0700 (PDT), Jerry He <rebound1618 at yahoo.com> wrote:

>Hi, 
>  Is it possible to create a function that you can use
>without parenthesizing the arguments? for example, for
>
>def examine(str):
>     .....
>     .....
>
>Is there some way to define it so that I can call it
>like
>
>examine "string" 
>instead of examine("string")? 
>
I suggested in a previous thread that one could support such a syntax by
supporting an invisible binary operator between two expressions, so that
examine "string" translates to examine.__invisbinop__("string") if
examine as an expression evaluates to an object that has a __invisbinop__ method.

Then you wouldn't define examine as a function, you would define it as an instance
of a class like
    class Examine(object):
        define __invisbinop__(self, other):
            #...whatever
    examine = Examine()
and then
    examine "string"

Of course this is a strange syntax mod to the language, and undoubtedly
would have many strange effects, but superficially, it seems like something
could be done. But a consequence of white space as potential invisible operator
would be that foo() and foo () might be foo.__call__() vs foo.__invisbinop_(())

Regards,
Bengt Richter



More information about the Python-list mailing list