Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104384 > unrolled thread
| Started by | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| First post | 2016-03-09 10:57 +0530 |
| Last post | 2016-03-09 17:34 +1100 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
exec "x = 3; print x" in a - How does it work? "Veek. M" <vek.m1234@gmail.com> - 2016-03-09 10:57 +0530
Re: exec "x = 3; print x" in a - How does it work? Ben Finney <ben+python@benfinney.id.au> - 2016-03-09 16:50 +1100
Re: exec "x = 3; print x" in a - How does it work? "Veek. M" <vek.m1234@gmail.com> - 2016-03-09 11:51 +0530
Re: exec "x = 3; print x" in a - How does it work? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-09 16:54 +1100
Re: exec "x = 3; print x" in a - How does it work? "Veek. M" <vek.m1234@gmail.com> - 2016-03-09 11:55 +0530
Re: exec "x = 3; print x" in a - How does it work? Chris Angelico <rosuav@gmail.com> - 2016-03-09 17:34 +1100
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-03-09 10:57 +0530 |
| Subject | exec "x = 3; print x" in a - How does it work? |
| Message-ID | <nboc29$up4$1@dont-email.me> |
What is the return value of `exec`? Would that object be then used to iterate the sequence in 'a'? I'm reading this: https://www.python.org/download/releases/2.2.3/descrintro/
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-03-09 16:50 +1100 |
| Message-ID | <mailman.68.1457502660.15725.python-list@python.org> |
| In reply to | #104384 |
"Veek. M" <vek.m1234@gmail.com> writes: > What is the return value of `exec`? You can refer to the documentation for questions like that. <URL:https://docs.python.org/3/library/functions.html#exec> > Would that object be then used to iterate the sequence in 'a'? The ‘for’ or ‘while’ statements are correct for iteration. > I'm reading this: > https://www.python.org/download/releases/2.2.3/descrintro/ Why? -- \ “Generally speaking, the errors in religion are dangerous; | `\ those in philosophy only ridiculous.” —David Hume, _A Treatise | _o__) of Human Nature_, 1739 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-03-09 11:51 +0530 |
| Message-ID | <nbof84$6mo$1@dont-email.me> |
| In reply to | #104385 |
Ben Finney wrote: > "Veek. M" <vek.m1234@gmail.com> writes: > >> What is the return value of `exec`? > > You can refer to the documentation for questions like that. > <URL:https://docs.python.org/3/library/functions.html#exec> > >> Would that object be then used to iterate the sequence in 'a'? > > The ‘for’ or ‘while’ statements are correct for iteration. > >> I'm reading this: >> https://www.python.org/download/releases/2.2.3/descrintro/ > > Why? > Well, i had a question on MRO and asked on freenode #python and they suggested i read that. My question was: class A(x, y, z): pass class x(q,w,r) pass I wanted to know how the __mro__ would be generated (c3 linearization) I had assumed when using super() it would do: x, q, w, r, y, z, object basically hit a base class and finish with all it's parents before stepping to the next sibling But then i read somewhere else that it's like this: x, y, z, q, w, r, object and of course if q has base-classes then: x, y, z, q, w, r, e, f, g which is utterly confusing because you can't tell by looking where e, f, g are coming from (q) in this case.. This doc: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ I was getting all cross-eyed reading this: 'The ancestor tree for our new class is: LoggingOD, LoggingDict, OrderedDict, dict, object. For our purposes, the important result is that OrderedDict was inserted after LoggingDict and before dict! This means that the super() call in LoggingDict.__setitem__ now dispatches the key/value update to OrderedDict instead of dict.' I was wondering why they had chosen this notation over the other. And if you're wondering why that paper - because i was reading Beazley super() pg 120 and the whole hard-coding avoided using super() bit.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-03-09 16:54 +1100 |
| Message-ID | <56dfba89$0$2805$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #104384 |
On Wednesday 09 March 2016 16:27, Veek. M wrote:
> What is the return value of `exec`? Would that object be then used to
> iterate the sequence in 'a'? I'm reading this:
> https://www.python.org/download/releases/2.2.3/descrintro/
exec is a statement, not a function, so it doesn't have a return value.
Are you referring to this line of code?
exec "x = 3; print x" in a
That doesn't return a value. The "in a" part tells exec which namespace to
execute the code in. It doesn't mean "test if the result is found inside
sequence a".
py> namespace = {'__builtins__': None}
py> exec "x = 3" in namespace
py> namespace
{'__builtins__': None, 'x': 3}
If you leave the "in namespace" part out, then exec will use the current
namespace, and x will become a local variable.
What happens if you don't put the special __builtins__ key into the
namespace? Python adds it for you:
py> mydict = {}
py> exec "foo = 99.99" in mydict
py> mydict.keys()
['__builtins__', 'foo']
What's inside __builtins__? Every single built-in function and class:
py> mydict['__builtins__']
{'bytearray': <type 'bytearray'>, 'IndexError': <type
'exceptions.IndexError'>, 'all': <built-in function all>,
... dozens more entries ...
'OverflowError': <type 'exceptions.OverflowError'>}
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-03-09 11:55 +0530 |
| Message-ID | <nboffi$6mo$2@dont-email.me> |
| In reply to | #104386 |
Steven D'Aprano wrote:
> On Wednesday 09 March 2016 16:27, Veek. M wrote:
>
>> What is the return value of `exec`? Would that object be then used to
>> iterate the sequence in 'a'? I'm reading this:
>> https://www.python.org/download/releases/2.2.3/descrintro/
>
>
> exec is a statement, not a function, so it doesn't have a return
> value.
>
> Are you referring to this line of code?
>
> exec "x = 3; print x" in a
>
>
> That doesn't return a value. The "in a" part tells exec which
> namespace to execute the code in. It doesn't mean "test if the result
> is found inside sequence a".
>
> py> namespace = {'__builtins__': None}
> py> exec "x = 3" in namespace
> py> namespace
> {'__builtins__': None, 'x': 3}
>
>
> If you leave the "in namespace" part out, then exec will use the
> current namespace, and x will become a local variable.
>
>
> What happens if you don't put the special __builtins__ key into the
> namespace? Python adds it for you:
>
>
> py> mydict = {}
> py> exec "foo = 99.99" in mydict
> py> mydict.keys()
> ['__builtins__', 'foo']
>
>
>
> What's inside __builtins__? Every single built-in function and class:
>
> py> mydict['__builtins__']
> {'bytearray': <type 'bytearray'>, 'IndexError': <type
> 'exceptions.IndexError'>, 'all': <built-in function all>,
>
> ... dozens more entries ...
>
> 'OverflowError': <type 'exceptions.OverflowError'>}
>
>
>
>
ah, okay - i'm familiar with the py3 syntax where you do:
eval('whatever stmt', globals={}, locals={})
I suppose this: exec " " in NS; syntax is strictly py2?
Many thanks :)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-09 17:34 +1100 |
| Message-ID | <mailman.69.1457505285.15725.python-list@python.org> |
| In reply to | #104389 |
On Wed, Mar 9, 2016 at 5:25 PM, Veek. M <vek.m1234@gmail.com> wrote:
> ah, okay - i'm familiar with the py3 syntax where you do:
> eval('whatever stmt', globals={}, locals={})
> I suppose this: exec " " in NS; syntax is strictly py2?
>
> Many thanks :)
Yeah, that's one of the things that was cleaned up in Py3. Several
pieces of magic syntax were replaced with perfectly ordinary
functions. While there are plenty of people who complain about having
to put parentheses around their print calls, I'm firmly of the opinion
that print(..., file=somefile) is WAY better than the >> syntax that
Py2 used. I can never remember whether it has to go first or has to go
last, and whether it's >> or >>>, etc, etc. It's magic syntax. Py3?
It's a keyword argument. Easy!
ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web