python -c "..." should output result like the interpreter
Hello all, I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does. The following outputs nothing: python -c "12 / 4.1" So you always have to prefix expressions with print to see the result. Michael -- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On 29 December 2010 13:36, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
The following outputs nothing:
python -c "12 / 4.1"
So you always have to prefix expressions with print to see the result.
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value? So you'd have python -e "12 / 4.1" (AFAICT, -e is unused at present). Paul.
On 29 December 2010 14:40, Paul Moore <p.f.moore@gmail.com> wrote:
On 29 December 2010 13:36, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
The following outputs nothing:
python -c "12 / 4.1"
So you always have to prefix expressions with print to see the result.
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value? So you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
That would be great. I did worry that changing the output would be backwards incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour. I use this trick for finding the source code of Python modules: $ python -c "import os;print os.__file__[:-1]" /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py Often in the form: $ mate `python -c "import module;print module.__file__[:-1]"` All the best, Michael Foord
Paul. _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On 29 December 2010 14:46, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
That would be great. I did worry that changing the output would be backwards incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
I use this trick for finding the source code of Python modules:
$ python -c "import os;print os.__file__[:-1]" /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py
I'd suggest allowing multiple -c and -e options, interspersed as needed, with each being treated as a separate line, so you could do $ python -c "import os" -e "os.__file__[:-1]" (Actually, I'd find "python -c stmt1 -c stmt2" mildly more readable than "python -c "stmt1; stmt2"" so that's another (equally small) benefit to me...) Paul. PS I take Antoine's point that it's a small benefit, but like you, I find myself needing it quite a lot in practice :-)
On 29 December 2010 15:09, Paul Moore <p.f.moore@gmail.com> wrote:
On 29 December 2010 14:46, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
That would be great. I did worry that changing the output would be backwards incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
I use this trick for finding the source code of Python modules:
$ python -c "import os;print os.__file__[:-1]" /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py
I'd suggest allowing multiple -c and -e options, interspersed as needed, with each being treated as a separate line, so you could do
$ python -c "import os" -e "os.__file__[:-1]"
(Actually, I'd find "python -c stmt1 -c stmt2" mildly more readable than "python -c "stmt1; stmt2"" so that's another (equally small) benefit to me...)
Well, I'd like both. :-)
Paul.
PS I take Antoine's point that it's a small benefit, but like you, I find myself needing it quite a lot in practice :-)
Ditto. Michael Foord -- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
Am 29.12.2010 16:13, schrieb Michael Foord:
I'd suggest allowing multiple -c and -e options, interspersed as needed, with each being treated as a separate line, so you could do
$ python -c "import os" -e "os.__file__[:-1]"
(Actually, I'd find "python -c stmt1 -c stmt2" mildly more readable than "python -c "stmt1; stmt2"" so that's another (equally small) benefit to me...)
Well, I'd like both. :-)
That's another incompatible change though: currently, sys.argv is filled with everything after the argument of -c. Georg
Am 29.12.2010 15:46, schrieb Michael Foord:
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value? So you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
That would be great. I did worry that changing the output would be backwards incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
Hey, what about this little module: import sys for x in sys.argv[1:]: exec compile(x, '<cmdline>', 'single') Then: $ python -me '1+1; 2+2' 2 4 Georg
On 29 December 2010 15:18, Georg Brandl <g.brandl@gmx.net> wrote:
Am 29.12.2010 15:46, schrieb Michael Foord:
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value?
So
you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
That would be great. I did worry that changing the output would be
backwards
incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
Hey, what about this little module:
import sys for x in sys.argv[1:]: exec compile(x, '<cmdline>', 'single')
Then:
$ python -me '1+1; 2+2' 2 4
I *really* like that. :-) Added it to my standard Python installs. Thanks Michael
Georg
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On 29 December 2010 15:18, Georg Brandl <g.brandl@gmx.net> wrote:
Am 29.12.2010 15:46, schrieb Michael Foord:
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value?
So
you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
That would be great. I did worry that changing the output would be
backwards
incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
Hey, what about this little module:
import sys for x in sys.argv[1:]: exec compile(x, '<cmdline>', 'single')
Then:
$ python -me '1+1; 2+2' 2 4
So now you can `pip install e` and then `python -me`... Michael
Georg
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On 29 December 2010 15:45, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
On 29 December 2010 15:18, Georg Brandl <g.brandl@gmx.net> wrote:
Am 29.12.2010 15:46, schrieb Michael Foord:
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value?
So
you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
That would be great. I did worry that changing the output would be
backwards
incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
Hey, what about this little module:
import sys for x in sys.argv[1:]: exec compile(x, '<cmdline>', 'single')
Then:
$ python -me '1+1; 2+2' 2 4
So now you can `pip install e` and then `python -me`...
http://pypi.python.org/pypi/e As an added bonus if the first argument is a module name it will print out the location of the module on the filesystem. Michael
Michael
Georg
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
--
May you do good and not evil May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On 29 December 2010 15:45, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
On 29 December 2010 15:18, Georg Brandl <g.brandl@gmx.net> wrote:
Am 29.12.2010 15:46, schrieb Michael Foord:
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value?
So
you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
That would be great. I did worry that changing the output would be
backwards
incompatible with code that shells out to Python using "-c", so a different command line option would be great. So long as it works with multiple statements (semi-colon separated) like the current "-c" behaviour.
Hey, what about this little module:
import sys for x in sys.argv[1:]: exec compile(x, '<cmdline>', 'single')
Then:
$ python -me '1+1; 2+2' 2 4
So now you can `pip install e` and then `python -me`...
Just as a follow up, for which we should still be blaming Georg, you can now do `pip install oo` followed by `python -moo`. (Requires pygame - tested on Linux and Mac should be cross platform.) All the best, Michael Foord
Michael
Georg
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
--
May you do good and not evil May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On Wed, Dec 29, 2010 at 9:40 AM, Paul Moore <p.f.moore@gmail.com> wrote:
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value?
+1 Changing -c would be unacceptable. -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> "A storm broke loose in my mind." --Albert Einstein
On 29Dec2010 10:27, Fred Drake <fdrake@acm.org> wrote: | On Wed, Dec 29, 2010 at 9:40 AM, Paul Moore <p.f.moore@gmail.com> wrote: | > I like the idea, but that's a fairly big semantic change. What about | > adding an -e option that takes an expression, and prints its value? | | +1 | | Changing -c would be unacceptable. Likewise. My -1 on having -c output the final value is as for the shell: does "sh -c sh-cmd" have an implied echo? No, and lucky we are that it does not. Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. - Brian W. Kernighan
On Wed, Dec 29, 2010 at 9:40 AM, Paul Moore <p.f.moore@gmail.com> wrote: ..
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value? So you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
+1 For some reason I always type -e (for "eval" or "exec"?) before realizing that it should have been -c. What do you think python -e "for i in range(5): i" should print? Note that on >>> prompt:
for i in range(5): i ... 0 1 2 3 4
On 29 December 2010 16:35, Alexander Belopolsky < alexander.belopolsky@gmail.com> wrote:
On Wed, Dec 29, 2010 at 9:40 AM, Paul Moore <p.f.moore@gmail.com> wrote: ..
I like the idea, but that's a fairly big semantic change. What about adding an -e option that takes an expression, and prints its value? So you'd have
python -e "12 / 4.1"
(AFAICT, -e is unused at present).
+1
For some reason I always type -e (for "eval" or "exec"?) before realizing that it should have been -c.
What do you think python -e "for i in range(5): i" should print? Note that on >>> prompt:
for i in range(5): i ... 0 1 2 3 4
I'd be happy with the same behaviour as the interactive interpreter. Michael
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On Wed, 29 Dec 2010 13:36:53 +0000 Michael Foord <fuzzyman@voidspace.org.uk> wrote:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
The following outputs nothing:
python -c "12 / 4.1"
So you always have to prefix expressions with print to see the result.
Is sparing the need to type print() really worthwhile here? I often leave a Python interpreter prompt open in a terminal so that I can try out whatever I need there, so I don't have to type "python -c", I can reuse previous results and imports, etc. Unless you are severaly RAM-limited (or OS X has poor multiwindowing capabilities ;-)), this should probably suit you as well. Regards Antoine.
On 29 December 2010 14:55, Antoine Pitrou <solipsis@pitrou.net> wrote:
On Wed, 29 Dec 2010 13:36:53 +0000 Michael Foord <fuzzyman@voidspace.org.uk> wrote:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
The following outputs nothing:
python -c "12 / 4.1"
So you always have to prefix expressions with print to see the result.
Is sparing the need to type print() really worthwhile here?
Well, what usually happens is that I forget I have to print and do it without first. Then when there is no output I remember and redo with the print in place. Making the print unnecessary would therefore mean substantially less than half as much typing.... Michael
I often leave a Python interpreter prompt open in a terminal so that I can try out whatever I need there, so I don't have to type "python -c", I can reuse previous results and imports, etc. Unless you are severaly RAM-limited (or OS X has poor multiwindowing capabilities ;-)), this should probably suit you as well.
Regards
Antoine.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
Am 29.12.2010 14:36, schrieb Michael Foord:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
If it did, you couldn't do this anymore (with the obvious implementation): python -c 'print 1 print 2' which I guess a few people do rely upon. A new -e option would be fine, but -- YAGNI? Georg
On Wed, 29 Dec 2010 16:13:15 +0100 Georg Brandl <g.brandl@gmx.net> wrote:
Am 29.12.2010 14:36, schrieb Michael Foord:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
If it did, you couldn't do this anymore (with the obvious implementation):
python -c 'print 1 print 2'
which I guess a few people do rely upon.
A new -e option would be fine, but -- YAGNI?
Apparently, MIGNI.
On 29 December 2010 15:20, Antoine Pitrou <solipsis@pitrou.net> wrote:
On Wed, 29 Dec 2010 16:13:15 +0100 Georg Brandl <g.brandl@gmx.net> wrote:
Am 29.12.2010 14:36, schrieb Michael Foord:
Hello all,
I often use python as a calculator or for simple operations using -c. It would be enormously useful if python -c "..." would output on stdout the result of the last evaluated expression in the same way that the interactive interpreter does.
If it did, you couldn't do this anymore (with the obvious implementation):
python -c 'print 1 print 2'
which I guess a few people do rely upon.
A new -e option would be fine, but -- YAGNI?
Apparently, MIGNI.
Hah. :-) And PIGNI. Michael
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
participants (7)
-
Alexander Belopolsky
-
Antoine Pitrou
-
Cameron Simpson
-
Fred Drake
-
Georg Brandl
-
Michael Foord
-
Paul Moore