Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is an interactive command a block?

2 views
Skip to first unread message

Alf P. Steinbach

unread,
Nov 19, 2009, 3:37:17 PM11/19/09
to
The CPython 3.1.1 language reference �4.1 says

"Each command typed interactively is a block."

It also says

"If a name is bound in a block, it is a local variable of that block, unless
declared as nonlocal"

Even with a non-literal try-for-best-meaning reading I can't get this to mesh
with the actual behavior of the interpreter, e.g.

>>> for x in "poi":
... fandango = 666
...
>>> fandango
666
>>> _

My current understanding is (A) that the interpreter is correct in this respect
(for one would harldly want the effects of statements to be fundamentally
different in interpreted mode, except the presentation of expression results),
and (B), but here I'm less sure, that the documentation is incorrect.

So what I'm asking about is mainly (B), because if the documentation is correct
after all, then there's something I haven't grokked. :-)


Cheers,

- Alf

Steven D'Aprano

unread,
Nov 19, 2009, 4:08:15 PM11/19/09
to
On Thu, 19 Nov 2009 21:37:17 +0100, Alf P. Steinbach wrote:

> The CPython 3.1.1 language reference §4.1 says
>
> "Each command typed interactively is a block."
>
> It also says
>
> "If a name is bound in a block, it is a local variable of that block,
> unless
> declared as nonlocal"
>
> Even with a non-literal try-for-best-meaning reading I can't get this to
> mesh with the actual behavior of the interpreter, e.g.
>
> >>> for x in "poi":
> ... fandango = 666
> ...
> >>> fandango
> 666
> >>> _
>
> My current understanding is (A) that the interpreter is correct in this
> respect (for one would harldly want the effects of statements to be
> fundamentally different in interpreted mode, except the presentation of
> expression results), and (B), but here I'm less sure, that the
> documentation is incorrect.


Why do you say that? I don't see what it is in the command you typed that
leads you to think the documentation is incorrect.

The first command you type is:

for x in "poi":
fandango = 666


which binds two names, x and fandango. Since you are not typing them in a
function or class definition, locals() is globals() and the two local
names you create happen to also be globals.

The next two commands you type:

fandango
_

don't bind anything, so aren't relevant.

If it helps:


>>> for x in "poi":
... fandango = 666
...

>>> locals() is globals()
True
>>> fandango
666
>>> locals()['fandango']
666
>>> import __main__
>>> __main__.fandango
666

--
Steven

Alf P. Steinbach

unread,
Nov 19, 2009, 4:42:31 PM11/19/09
to
* Steven D'Aprano:

Thanks, that may be it.

In most other languages I'm familiar with, if a name is local then it isn't
global (and vice versa), and I had that as an underlying assumption since it
doesn't appear to be mentioned in the documentation.

However, I find a language reference statement that alludes to this: "(The
variables of the module code block are local and global.)"

Hm...


> The next two commands you type:
>
> fandango
> _
>
> don't bind anything, so aren't relevant.

Well it showed that 'fandango' had been defined as a global. :-)


> If it helps:
>
>
>>>> for x in "poi":
> ... fandango = 666
> ...
>>>> locals() is globals()
> True
>>>> fandango
> 666
>>>> locals()['fandango']
> 666
>>>> import __main__
>>>> __main__.fandango
> 666

Yeah, helps.

I feel that there's still something lacking in my understanding though, like
how/where the "really actually just pure local not also global" is defined for
function definition, but it's now just a vague feeling of something missing, not
a feeling of direct contradiction as I had when I believed local != global.


Cheers, & thanks,

- Alf

Benjamin Kaplan

unread,
Nov 20, 2009, 8:06:24 AM11/20/09
to pytho...@python.org

It's because the only blocks in python that have their own scope are
classes and functions. For loops don't have their own scope- they use
the enclosing one, which in this case is globals.

>
> Cheers, & thanks,
>
> - Alf

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Alf P. Steinbach

unread,
Nov 20, 2009, 10:23:48 AM11/20/09
to
* Benjamin Kaplan:

> On Thu, Nov 19, 2009 at 4:42 PM, Alf P. Steinbach <al...@start.no> wrote:
>> * Steven D'Aprano:
>>
>> I feel that there's still something lacking in my understanding though, like
>> how/where the "really actually just pure local not also global" is defined
>> for function definition, but it's now just a vague feeling of something
>> missing, not a feeling of direct contradiction as I had when I believed
>> local != global.
>>
>
> It's because the only blocks in python that have their own scope are
> classes and functions. For loops don't have their own scope- they use
> the enclosing one, which in this case is globals.

Thanks, but hey, contradiction: you mention globals as an "enclosing" scope,
i.e. that a module can have a scope, while stating that only classes and
functions have their own scope (so, would a module have it's not own scope?).

I think, having now read up and down and sideways in the docs, that the proper
term in Python is "namespace", and that "scope" in Python refers to where the
names in a namespace are directly accessible, with a special case for classes,
namely that the scope of a class' namespace doesn't penetrate down into function
definitions, hence problem with comprehensions expressed directly in a class
definition. I believe that's a so called language "wart" -- which C++ is full
of, just now discovering some of Python's :-). It's almost like C++'s "most
vexing parse", a natural and apparently meaningful source code construct that
due to the detailed rules turns out to be meaningless. I wish language designers
could be a little less hooked on low level consistency. Because it makes for
high level inconsistency, very difficult to explain without zillions of details.

But anyways, it's starting to make more sense, yes.


Cheers,

- Alf

Ethan Furman

unread,
Nov 20, 2009, 11:02:38 AM11/20/09
to pytho...@python.org
Alf P. Steinbach wrote:
> * Benjamin Kaplan:
>> On Thu, Nov 19, 2009 at 4:42 PM, Alf P. Steinbach <al...@start.no> wrote:
>>>
>>> I feel that there's still something lacking in my understanding
>>> though, like
>>> how/where the "really actually just pure local not also global" is
>>> defined
>>> for function definition, but it's now just a vague feeling of something
>>> missing, not a feeling of direct contradiction as I had when I believed
>>> local != global.
>>>
>>
>> It's because the only blocks in python that have their own scope are
>> classes and functions. For loops don't have their own scope- they use
>> the enclosing one, which in this case is globals.
>
>
> Thanks, but hey, contradiction: you mention globals as an "enclosing"
> scope, i.e. that a module can have a scope, while stating that only
> classes and functions have their own scope (so, would a module have it's
> not own scope?).

module scope == global scope

That is, there is nothing higher than module scope. (So, yes, global is
a slight misnomer... in Python it means 'global to a module'.)

~Ethan~

Steven D'Aprano

unread,
Nov 20, 2009, 3:37:12 PM11/20/09
to
On Fri, 20 Nov 2009 08:02:38 -0800, Ethan Furman wrote:

>> Thanks, but hey, contradiction: you mention globals as an "enclosing"
>> scope, i.e. that a module can have a scope, while stating that only
>> classes and functions have their own scope (so, would a module have
>> it's not own scope?).
>
> module scope == global scope
>
> That is, there is nothing higher than module scope. (So, yes, global is
> a slight misnomer... in Python it means 'global to a module'.)

Actually there is: built-ins.

That's why you can access (e.g.) len without having to define it, or
import it, first. And what's more, you can do this:


>>> len([1,2,3])
3
>>> def len(x): # Shadow the built-in
... return -3
...
>>> len([1,2,3])
-3
>>> del len
>>> len([1,2,3]) # It's back!
3

--
Steven

Ethan Furman

unread,
Nov 20, 2009, 4:03:43 PM11/20/09
to pytho...@python.org

Very good point, I had forgotten. Of course, injecting into built-ins
is not recommended.

~Ethan~

0 new messages