call function in console without paranthesis

Robert Kern robert.kern at gmail.com
Thu May 31 20:07:58 EDT 2007


Troels Thomsen wrote:
> Hello,
> 
> I am wondering if I can write some code, that allows me to call functions in 
> the console , IDLE, without using the paranthesis notation. Like print.
> This will improve "intreractive'ness"
> 
> serialOpen() # some magic is issued here !!!
> tx Hello
> 
> instead of
> serialObj = mySerial(....)
> serialObj.Tx("Hello")

Take a look at IPython. It has an autocall mode that allows this.

  http://ipython.scipy.org/moin/

In [1]: %autocall?
Type:           Magic function
Base Class:     <type 'instancemethod'>
Namespace:      IPython internal
File:           /Users/rkern/svn/ipython/IPython/Magic.py
Definition:     %autocall(self, parameter_s='')
Docstring:
    Make functions callable without having to type parentheses.

    Usage:

       %autocall [mode]

    The mode can be one of: 0->Off, 1->Smart, 2->Full.  If not given, the
    value is toggled on and off (remembering the previous state).


In [2]: %autocall 2
Automatic calling is: Full

In [3]: def f(*args):
   ...:     print args
   ...:
   ...:

In [4]: f
------> f()
()

In [5]: f 1
------> f(1)
(1,)

In [7]: f 'Foo', 'bar'
------> f('Foo', 'bar')
('Foo', 'bar')

In [9]: f 1, 2, 3
------> f(1, 2, 3)
(1, 2, 3)

In [13]: x = 1

In [14]: f x
-------> f(x)
(1,)


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list