Naked function call syntax

Patrick K. O'Brien pobrien at orbtech.com
Sun Jul 15 13:35:24 EDT 2001


Here is a class that might give you some ideas. If you combined this with
raw_input('What do you want to pd?') as the method you might get something
close to what you want. I haven't actually used this class with something
that needed a parameter but it should be doable.

class PseudoKeyword:
    '''A callable class that implements a method that is 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):
        self.method()

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

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


"Bengt Richter" <bokr at accessone.com> wrote in message
news:3b4fa037.1845423078 at wa.news.verio.net...
> Interactively, I find myself writing little things like:
>
> def pd(x):
>     try:
>         print x.__doc__
>     except:
>         print '(no doc string)'
>
> so that, e.g., I can type
>
>     pd(vars)
>
> and get formatted info. But I'm lazy enough that
> I would like to to type just
>     pd vars
>
> Is there an existing pythonic way to do this?
>
> If not, would there be breakage if python were modified to make
>
>     foo whatever-up-to-EOL
>
> equivalent to
>
>     foo(whatever-up-to-EOL)
>
> when it would be a syntax error otherwise? I.e., I'm wondering
> if the grammar could be modified to do this by changing trailer
> to accept arglist NEWLINE if _all_ else fails, and treat a match
> as if it was an arglist. Leading '(' or '[' ambiguity would be
> resolved in favor of normal arglist or subscriptlist.
>
>





More information about the Python-list mailing list