Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-04 Thread Steven D'Aprano
On Tue, Nov 04, 2014 at 04:11:13PM +, Albert-Jan Roskam wrote: > Hmm, I get 1900 occurrences of eval() (and 700 of frozenset, just > curious) in Python. That's MUCH, I must be something wrong, but I am > rushing now! For what it's worth, in Python 2.7, and *only* looking at the top level o

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-04 Thread Alan Gauld
On 04/11/14 16:11, Albert-Jan Roskam wrote: Hmm, I get 1900 occurrences of eval() Try printing the filenames too. A lot of those will be in Tkinter/Tix which uses tcl.eval() to execute the underlying Tcl/Tk widget code. Also I suspect a lot of the 'introspection' type modules that are not int

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-04 Thread Albert-Jan Roskam
- Original Message - > From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Tuesday, November 4, 2014 4:08 AM > Subject: Re: [Tutor] eval use (directly by interpreter vs with in a script) > > On Mon, Nov 03, 2014 at 09:33:18AM -0800, Albert-Jan Ros

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Steven D'Aprano
On Mon, Nov 03, 2014 at 09:33:18AM -0800, Albert-Jan Roskam wrote: > >Real question is what you're trying to do. eval() and exec() are to be > >avoided if possible, so the solutions are not necessarily the easiest. > > I sometimes do something like > ifelse = "'teddybear' if bmi > 30 else 'skin

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Steven D'Aprano
On Sun, Nov 02, 2014 at 06:23:12PM -0500, Ken G. wrote: > I use exec to jump to another program within the > same directory, such as: > > execfile("BloodPressure02Sorting.py") > > and let the program terminate there. Should I do > it differently or are you talking about a different > horse? Tha

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Danny Yoo
On Mon Nov 03 2014 at 10:48:29 AM Danny Yoo wrote: > On Mon Nov 03 2014 at 10:04:41 AM Alan Gauld > wrote: > >> On 03/11/14 17:33, Albert-Jan Roskam wrote: >> >> > I sometimes do something like >> > ifelse = "'teddybear' if bmi > 30 else 'skinny'" >> > weightcats = [eval(ifelse) for bmi in bmis]

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Danny Yoo
On Mon Nov 03 2014 at 10:04:41 AM Alan Gauld wrote: > On 03/11/14 17:33, Albert-Jan Roskam wrote: > > > I sometimes do something like > > ifelse = "'teddybear' if bmi > 30 else 'skinny'" > > weightcats = [eval(ifelse) for bmi in bmis] > > > > Would this also be a *bad* use of eval? It can be avoi

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Alan Gauld
On 03/11/14 17:33, Albert-Jan Roskam wrote: I sometimes do something like ifelse = "'teddybear' if bmi > 30 else 'skinny'" weightcats = [eval(ifelse) for bmi in bmis] Would this also be a *bad* use of eval? It can be avoided, but this is so concise. eval etc are worst where the code to be ev

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Albert-Jan Roskam
> >Real question is what you're trying to do. eval() and exec() are to be >avoided if possible, so the solutions are not necessarily the easiest. I sometimes do something like ifelse = "'teddybear' if bmi > 30 else 'skinny'" weightcats = [eval(ifelse) for bmi in bmis] Would this also be a *b

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-03 Thread Ken G.
On 11/03/2014 12:37 AM, Danny Yoo wrote: I use exec to jump to another program within the same directory, such as: execfile("BloodPressure02Sorting.py") and let the program terminate there. Should I do it differently or are you talking about a different horse? This is related. Rather than u

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Danny Yoo
> I use exec to jump to another program within the > same directory, such as: > > execfile("BloodPressure02Sorting.py") > > and let the program terminate there. Should I do > it differently or are you talking about a different > horse? This is related. Rather than use execfile, you may want to c

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Dave Angel
On 11/02/2014 03:01 PM, Alex Kleider wrote: I'm puzzled by the following behaviour: The following works as I expect: alex@x301:~/Python$ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. code = """\

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread eryksun
On Sun, Nov 2, 2014 at 3:07 PM, Peter Otten <__pete...@web.de> wrote: > Inside a function there is no direct way to get access to the local > namespace used by exec(). Indeed, exec() can't create new function locals, or even update the values of existing function locals. Another option for indire

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Alex Kleider
On 2014-11-02 13:49, Danny Yoo wrote: Are you just exploring the features of Python, or is there a particular task you're trying to solve with eval or exec()? Perhaps you can accomplish the same goal in another way? Thank you and also Peter for your help. The answer to your question is "'yes

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Ken G.
On 11/02/2014 04:49 PM, Danny Yoo wrote: Hi Alex, Just as a side note, someone has probably already told you something like this, but: I would strongly recommend not to use Python's eval() or exec(). Those language features are dangerous. Every eval() or exec() is a possible vector for injec

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Danny Yoo
Hi Alex, Just as a side note, someone has probably already told you something like this, but: I would strongly recommend not to use Python's eval() or exec(). Those language features are dangerous. Every eval() or exec() is a possible vector for injection attacks. This week's injection attack

Re: [Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Peter Otten
Alex Kleider wrote: > I'm puzzled by the following behaviour: > > The following works as I expect: > > alex@x301:~/Python$ python3 > Python 3.4.0 (default, Apr 11 2014, 13:05:18) > [GCC 4.8.2] on linux > Type "help", "copyright", "credits" or "license" for more information. code = """\ > ..

[Tutor] eval use (directly by interpreter vs with in a script)

2014-11-02 Thread Alex Kleider
I'm puzzled by the following behaviour: The following works as I expect: alex@x301:~/Python$ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. code = """\ ... s1 = 'first' ... s2 = 'second' ... """