By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

call function in console without paranthesis

P: n/a
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")

thx in advance
Troels
May 31 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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"
Matlab-like, right?

In a nutshell: No, not possible in python *itself*.
I'm not a python-guru, but I'm pretty sure that this would require
changes to the python sources, because what you ask for would basically
be a new language (syntax) feature.

But if you really need that, you could try to write your own parser. You
would have to think *hard* to get the semantics right, but in principle
all you would have to do is translate your "simplified" console-input to
valid python code and feed that to the exec() function (I think). At
least, that's how I would do it.

/W
May 31 '07 #2

P: n/a
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

Jun 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.