Scope in 2.2.1

Kragen Sitaker kragen at pobox.com
Wed May 15 16:47:58 EDT 2002


"David LeBlanc" <whisper at oz.net> writes:
> I was refering to a functional view of how python works and not how it
> actually works.

Me too.

> If what i'm told is true (and I don't think i've been
> mislead), I think that what one might call initializers or
> bind-with-assignment are effectively (if not actually) declarations:
> 	a = 0;

You can call them whatever you like, but most of us call them
"assignment statements".  And they sort of function as declarations,
yes, if they're not in global scope and there isn't a global statement
declaring the target global in the same block and the target is an
identifier and not an attribute ref, a subscription, or a slicing, and
with the caveat that two such "declarations" in the same block with
the same identifier declare the same variable, not two different
variables with the same name.  And, as you pointed out in the first
place, the scope of the "declaration" may in fact extend *above* the
declaration itself.

In short, you can think of assignment statements as a kind of
declaration, but it's likely to confuse you more than it enlightens
you.  It's easier to think that the compiler infers declarations in
places.

> declares (binds) a variable called "a" and assigns a value of 0 to it.

Declaration and binding are different, except in pure functional
languages.  Binding and assignment are the same.

> Although Python has a single pass sequential parser, it has some behaviors
> that act as though there are one or more pre-passes to discover bindings
> (and Terry Reedy mentioned another "pass" that recognizes generators).

Well, as I said in the post you are replying to, these passes don't
affect the parsing.

> Please see the posts by Terry Reedy (he refers to passes as "runtimes") and
> Tim Peters with this same subject.

I read them; Tim's post in particular was excellent.

> I don't really think of global as a binding, although I know that
> technically it is.

Technically it is not.  It is a scope, and there is also a global
statement that declares that a variable does not exist in the local
scope.  A "global binding" is just the binding of a variable that
happens to be global.

David LeBlanc wrote:
> [Kragen Sitaker wrote]
> > [David LeBlanc wrote]
> > > [Alex Martelli wrote]
> > > > the x in the print may reference either the same binding as
> > > > given by f's caller, or the binding to 45.  It IS certainly a 
> > > > reference to the *local variable* named x, but what binding of 
> > > > that local variable applies, it's anybody's guess.  "scoping" 
> > > > might be a useful neologism here.
> > >
> > > gaaaaa! this is mad! How can you write a sane program in this sort of
> > > environment?!?
> >
> > Goodness, David, I had no idea you were such a functional-programming
> > enthusiast.  :)
> 
> While functional programming may be a wonderful thing, my experience with
> functional programming languages has left me cold. I was recently encouraged
> to take a look at Haskell and my impression is that it makes Perl look
> positively lucid in comparison. I have also looked at ml and caml and was
> similarly impressed.

Well, the attribute of Python that you described as "mad" is precisely
the attribute that distinguishes pure functional languages like
Haskell from languages that support imperative programming, such as C,
C++, Pascal, Perl, SML, Caml, and Python.  (SML and Caml here are
debatable, because they don't support rebinding of variables, just
mutation of them.)  If you really think it's "mad", then you are
opposing the use of imperative programming languages.

It appears that you just didn't understand what Alex was talking
about, even after Tim's lucid explanation of how you didn't understand
it.  I thought it might be the case that you didn't understand when I
replied in the first place and thought I'd have a little fun, trusting
that you'd understand the joke when you came to understand what Alex
was talking about.  From your use of terminology in the post to which
I am replying, it appears that despite Tim's and Alex's explanations,
you still don't get it.  (Unless you're simply playing games and
insisting on using your own personal definition of "bind", pretending
that everybody else means the same thing you do by it.)  I hope the
present post is able to clear things up for you, but I doubt my
explanations can penetrate where Tim's and Alex's failed to penetrate.




More information about the Python-list mailing list