Would like to know if there is a way to call a function in python in repl just with the function name.
$ python -i interace.py
>>> load 834.png
>>> sharpen
>>> save
rather than
$ python -i interface.py
>>> load('834.png')
>>> sharpen()
>>> save()
reason being, I have a file instructions.txt
that has the format as above. I wish to just do $ python -i interface.py < instructions.txt
.
And for the times when I don't have an instruction file, I can just enter my instructions manually.
There's no way to do this with the regular Python interpreter. Function calls must include parentheses.
Now, your instructions look a lot like shell syntax, so maybe what you could do is write your own simple parser. Try looking at shlex to start.
By the way, if it helps, IPython includes an %autocall
setting that almost does what you want.
In [4]: print 'hello' File "<ipython-input-4-5a1ef41e7057>", line 1print 'hello'^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello')?In [5]: %autocall
Automatic calling is: SmartIn [6]: print 'hello'
------> print('hello')
helloIn [7]: print 834.png
------> print(834.png)File "<ipython-input-7-3d64d8523fdd>", line 1print(834.png)^
SyntaxError: invalid syntax