Scope in 2.2.1

David LeBlanc whisper at oz.net
Wed May 15 03:28:57 EDT 2002


<ahem>
I was refering to a functional view of how python works and not how it
actually works. 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;
declares (binds) a variable called "a" and assigns a value of 0 to it.
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).
Please see the posts by Terry Reedy (he refers to passes as "runtimes") and
Tim Peters with this same subject.

I don't really think of global as a binding, although I know that
technically it is. I think of it as "these names refer to variables bound in
the outer-most scope".

I regret having failed to adequately attribute Alex Martelli, a frequent
contributor to the python mailing list and c.l.p, as the author of the
quoted text.

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.

David LeBlanc
Seattle, WA USA

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Kragen Sitaker
> Sent: Monday, May 13, 2002 16:04
> To: python-list at python.org
> Subject: Re: Scope in 2.2.1
>
>
> "David LeBlanc" <whisper at oz.net> writes:
> > I have grown accustomed by long use to the way that C, C++ and
> Pascal (and
> > asm for that matter!) become aware of declarations. To use an analogy,
> > there's a line that moves down the page and things below the line are
> > unknown to the compiler since they're not seen yet unless
> they're forward
> > declared.
>
> Yes; unfortunately, this doesn't work in Python because Python doesn't
> have declarations, forward, backward, inside, outside, or upside down,
> except for the "global" statement.  If Python had declarations, we
> could avoid this whole mess.
>
> > Code is parsed based on what's known above the line. Python seems
> > to have an implicit per block pre-pass that gets all the bindings before
> > statements are parsed. Is this the correct idea?
>
> Python code is parsed sequentially, in a single pass; you will note
> that assignment to a global variable is syntactically identical to
> assignment to a local variable, and there is no declaration (like C's
> typedef and C++'s four other kinds of declarations that do the same
> thing) that will change the parsing of code.
>
> However, the bytecode generated by compiling a parsed statement may
> indeed depend on statements after it, and yes, this is sometimes
> surprising.
>
> > [David quotes somebody he neglects to credit, who signed "Alex"
> at the bottom]
> > > 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.  :)
>
> --
> http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list