evaluation question
Hi
This is probably a dumb newbie question but I've just started to learn
python3 and eval() isn't behaving as I'd expect in that it works for
some things and not others. eg:
>>> eval("1+1")
2
>>> eval("print(123)")
123
>>> eval("for i in range(1,10): i")
Traceback (most recent call last):
File "", line 1, in
File "", line 1
for i in range(1,10): i
^
SyntaxError: invalid syntax
Why did the 3rd one fail? Does it not handle complex expressions?
Thanks for any help
--
https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Fri, 27 Jan 2023 21:04:58 + Ben Bacarisse wrote: >[email protected] writes: > >> Hi > >It looks like you posted this question via Usenet. comp.lang.python is >essentially dead as a Usenet group. It exists, and gets NNTP versions >of mail sent to the mailing list, but nothing posted to the group via >NNTP get send on the mailing list. I prefer Usenet and dislike mailing >lists but that just means I can't really contribute to this "group" > >The "python-list" an an excellent resource (if you like the email >interface) and you can subscribe here: > >https://mail.python.org/mailman/listinfo/python-list>, > >> This is probably a dumb newbie question but I've just started to learn >> python3 and eval() isn't behaving as I'd expect in that it works for >> some things and not others. eg: >> > eval("1+1") >> 2 > eval("print(123)") >> 123 > eval("for i in range(1,10): i") >> Traceback (most recent call last): >> File "", line 1, in >> File "", line 1 >> for i in range(1,10): i >> ^ >> SyntaxError: invalid syntax >> >> Why did the 3rd one fail? Does it not handle complex expressions? > >It handles only expressions, and "for i in range(1,10): i" is not an >expression. You can use > exec("for i in range(1,10): i") Ok, thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Sat, 28 Jan 2023 14:22:01 +1300 dn wrote: >On 28/01/2023 05.37, [email protected] wrote: >> This is probably a dumb newbie question but I've just started to learn >> python3 and eval() isn't behaving as I'd expect in that it works for >> some things and not others. eg: >> > eval("1+1") >> 2 > eval("print(123)") >> 123 > eval("for i in range(1,10): i") >> Traceback (most recent call last): >>File "", line 1, in >>File "", line 1 >> for i in range(1,10): i >>^ >> SyntaxError: invalid syntax >> >> Why did the 3rd one fail? Does it not handle complex expressions? > >eval() is very powerful, and therefore rather dangerous in the risks it >presents. > >Thus, seems a strange/advanced question for a "newbie" to be asking. YMMV! Well ok, new-ish :) >Do you know about the Python REPL? Haven't learnt the acronyms yet. >If you open python within a terminal, each of the three >expressions/compound-statements listed will work, as desired, without >eval(). Umm, yeah, thats kind of obvious isn't it? -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Sun, 29 Jan 2023 23:57:51 -0500 Thomas Passin wrote: >On 1/29/2023 4:15 PM, [email protected] wrote: >> On 2023-01-28, Louis Krupp wrote: >>> On 1/27/2023 9:37 AM, [email protected] wrote: >> >> >>> eval("print(123)") 123 >> >> >> Does OP expect the text to come from the eval or from the print? >> > x = print( [i for i in range(1, 10)] ) >> [1, 2, 3, 4, 5, 6, 7, 8, 9] >> > x >> (nothing printed) > >Because print() returns nothing (i.e., the statement x is None is True). I don't understand this. What was the point of the upheaval of converting the print command in python 2 into a function in python 3 if as a function print() doesn't return anything useful? Surely even the length of the formatted string as per C's sprintf() function would be helpful? -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Tue, 31 Jan 2023 12:57:33 +1300 Greg Ewing wrote: >On 30/01/23 10:41 pm, [email protected] wrote: >> What was the point of the upheaval of converting >> the print command in python 2 into a function in python 3 if as a function >> print() doesn't return anything useful? > >It was made a function because there's no good reason for it >to have special syntax in the language. All languages have their ugly corners due to initial design mistakes and/or constraints. Eg: java with the special behaviour of its string class, C++ with "=0" pure virtual declaration. But they don't dump them and make all old code suddenly cease to execute. Pragmatism should always come before language purity. -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Wed, 1 Feb 2023 13:17:33 +1300 dn wrote: >On 01/02/2023 11.59, Greg Ewing wrote: >> On 31/01/23 10:24 pm, [email protected] wrote: >>> All languages have their ugly corners due to initial design mistakes >>> and/or >>> constraints. Eg: java with the special behaviour of its string class, C++ >>> with "=0" pure virtual declaration. But they don't dump them and make >>> all old >>> code suddenly cease to execute. >> >> No, but it was decided that Python 3 would have to be backwards >> incompatible, mainly to sort out the Unicode mess. Given that, >> the opportunity was taken to clean up some other mistakes as well. > >+1 >and the move to Unicode has opened-up the Python community beyond the >US, to embrace 'the world' - a proposition (still) not well-recognised >by (only) English-speakers/writers/readers. > > >Even though the proposition has a troll-bait smell to it:- > >1 nothing "ceased to execute" and Python 2 was maintained and developed >for quite some time and in-parallel to many Python 3 releases. MacOS only comes with python3 now. If you have a whole load of python2 code you want to run you now have to manually install python2 yourself. >2 the only constant in this business is 'change'. I'd rather cope with >an evolution in this language (which we know and love), than one day Its not evolution, its revolution. Evolution retains old functionality. -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Tue, 31 Jan 2023 21:00:53 + Mark Bourne wrote: >Greg Ewing wrote: >> On 30/01/23 10:41 pm, [email protected] wrote: >>> What was the point of the upheaval of converting >>> the print command in python 2 into a function in python 3 if as a >>> function >>> print() doesn't return anything useful? >> >> It was made a function because there's no good reason for it >> to have special syntax in the language. > >I think I saw somewhere that making print a function also had something >to do with being able to add extra keyword arguments like sep and end. >The syntax for printing to a specific file already seemed a bit odd with >the print statement, and adding extra arguments would have made it even >more clunky (yeah, I know ">>" is similar to C++ streams, but it looks >out of place in Python). Why couldn't they just keep "print" and call the function , oh I dunno, "printf" ? :) -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Wed, 1 Feb 2023 11:59:25 +1300 Greg Ewing wrote: >On 31/01/23 10:24 pm, [email protected] wrote: >> All languages have their ugly corners due to initial design mistakes and/or >> constraints. Eg: java with the special behaviour of its string class, C++ >> with "=0" pure virtual declaration. But they don't dump them and make all old > >> code suddenly cease to execute. > >No, but it was decided that Python 3 would have to be backwards >incompatible, mainly to sort out the Unicode mess. Given that, >the opportunity was taken to clean up some other mistakes as well. Unicode is just a string of bytes. C supports it with a few extra library functions to get unicode length vs byte length and similar. Its really not that hard. Rewriting an entire language just to support that sounds a bit absurd to me but hey ho... -- https://mail.python.org/mailman/listinfo/python-list
Re: evaluation question
On Wed, 1 Feb 2023 18:28:04 +0100 "Peter J. Holzer" wrote: >--b2nljkb3mdefsdhx >Content-Type: text/plain; charset=us-ascii >Content-Disposition: inline >Content-Transfer-Encoding: quoted-printable > >On 2023-02-01 09:00:39 -, [email protected] wrote: >> Its not evolution, its revolution. Evolution retains old functionality. > >Tell a penguin that it can fly :-) Yeah ok :) But the ancestors of penguins didn't wake up one morning, flap their wings and fall out the tree, it happened gradually. Python2 syntax could have been retained for X versions of 3 just as C++ keeps old stuff until its eventually deprecated them removed. -- https://mail.python.org/mailman/listinfo/python-list
