Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > block scope?

Reply
Thread Tools

block scope?

 
 
Paul Rubin
Guest
Posts: n/a
 
      04-07-2007
(Alex Martelli) writes:
> Thus the following example does not compile:
> class Test {
> public static void main(String[] args) {
> int i;
> for (int i = 0; i < 10; i++)


I'm ok with this; at the minimum, I think such nesting should produce
a warning message.
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      04-07-2007
Steve Holden <> wrote:

> What do you think the chances are of this being accepted for Python 3.0?
> It is indeed about the most rational approach, though of course it does
> cause problems with dynamic namespaces.


What problems do you have in mind? The compiler already determines the
set of names that are local variables for a function; all it needs to do
is diagnose an error or warning if the set of names for a nested
function overlaps with that of an outer one.

I shamefully admit that I haven't followed Python 3.0 discussions much
lately, so I don't really know what's planned on this issue.


Alex
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      04-07-2007
Paul Rubin <http://> wrote:

> (Alex Martelli) writes:
> > Thus the following example does not compile:
> > class Test {
> > public static void main(String[] args) {
> > int i;
> > for (int i = 0; i < 10; i++)

>
> I'm ok with this; at the minimum, I think such nesting should produce
> a warning message.


Yes, a warning could surely be a reasonable compromise.


Alex
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      04-07-2007
In article <1hw7kzo.1hepj3c1who5zhN%>,
Alex Martelli <> wrote:
>Steve Holden <> wrote:
>>
>> What do you think the chances are of this being accepted for Python 3.0?
>> It is indeed about the most rational approach, though of course it does
>> cause problems with dynamic namespaces.

>
>What problems do you have in mind? The compiler already determines the
>set of names that are local variables for a function; all it needs to do
>is diagnose an error or warning if the set of names for a nested
>function overlaps with that of an outer one.


exec?
--
Aahz () <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      04-08-2007
Paul Rubin wrote:
> John Nagle <> writes:
>
>> In a language with few declarations, it's probably best not to
>>have too many different nested scopes. Python has a reasonable
>>compromise in this area. Functions and classes have a scope, but
>>"if" and "for" do not. That works adequately.

>
>
> I think Perl did this pretty good. If you say "my $i" that declares
> $i to have block scope, and it's considered good practice to do this,
> but it's not required. You can say "for (my $i=0; $i < 5; $i++) { ... }"
> and that gives $i the same scope as the for loop. Come to think of it
> you can do something similar in C++.


Those languages have local declarations. "my" is a local
declaration. If you have explicit declarations, explict block
scope is no problem. Without that, there are problems. Consider

def foo(s, sname) :
if s is None :
result = ""
else :
result = s
msg = "Value of %s is %s" % (sname, result)
return(msg)

It's not that unusual in Python to initialize a variable on
two converging paths. With block scope, you'd break
code that did that.


John Nagle
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      04-08-2007
Aahz <> wrote:

> In article <1hw7kzo.1hepj3c1who5zhN%>,
> Alex Martelli <> wrote:
> >Steve Holden <> wrote:
> >>
> >> What do you think the chances are of this being accepted for Python 3.0?
> >> It is indeed about the most rational approach, though of course it does
> >> cause problems with dynamic namespaces.

> >
> >What problems do you have in mind? The compiler already determines the
> >set of names that are local variables for a function; all it needs to do
> >is diagnose an error or warning if the set of names for a nested
> >function overlaps with that of an outer one.

>
> exec?


option 1: that just runs the compiler a bit later -- thus transforming
ClashingVariableError into a runtime issue, exactly like it already does
for SyntaxError.

option 2: since a function containing any exec statement does not
benefit from the normal optimization of local variables, let it also
forgo the normal diagnosis of shadowed/clashing names.

option 3: extend the already-existing prohibition of mixing exec with
nested functions:

>>> def outer():

.... def inner(): return x
.... exec('x=23')
.... return inner()
....
File "<stdin>", line 3
SyntaxError: unqualified exec is not allowed in function 'outer' it
contains a nested function with free variables

to prohibit any mixing of exec and nested functions (not just those
cases where the nested function has free variables).


My personal favorite is option 3.


Alex
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      04-08-2007
(Alex Martelli) writes:
> > exec?

> option 1: that just runs the compiler a bit later ...


Besides exec, there's also locals(), i.e.
locals['x'] = 5
can shadow a variable. Any bad results are probably deserved
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      04-08-2007
On Apr 7, 8:50 am, James Stroud <jstr...@mbi.ucla.edu> wrote:
> Paul Rubin wrote:
> > John Nagle <n...@animats.com> writes:
> >> In a language with few declarations, it's probably best not to
> >> have too many different nested scopes. Python has a reasonable
> >> compromise in this area. Functions and classes have a scope, but
> >> "if" and "for" do not. That works adequately.

>
> > I think Perl did this pretty good. If you say "my $i" that declares
> > $i to have block scope, and it's considered good practice to do this,
> > but it's not required. You can say "for (my $i=0; $i < 5; $i++) { ... }"
> > and that gives $i the same scope as the for loop. Come to think of it
> > you can do something similar in C++.

>
> How then might one define a block? All lines at the same indent level
> and the lines nested within those lines?
>
> i = 5
> for my i in xrange(4):
> if i: # skips first when i is 0
> my i = 100
> if i:
> print i # of course 100
> break
> print i # i is between 0 & 3 here
> print i # i is 5 here
>
> Doesn't leave a particularly bad taste in one's mouth, I guess (except
> for the intended abuse).
>

How about something like this instead:

i = 5
block:
for i in xrange(4):
if i: # skips first when i is 0
block:
i = 100
if i:
print i # of course 100
break
print i # i is between 0 & 3 here
print i # i is 5 here

Any variable that's assigned to within a block would be local to that
block, as it is in functions.

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      04-08-2007
Paul Rubin <http://> wrote:

> (Alex Martelli) writes:
> > > exec?

> > option 1: that just runs the compiler a bit later ...

>
> Besides exec, there's also locals(), i.e.
> locals['x'] = 5
> can shadow a variable. Any bad results are probably deserved


>>> locals['x']=5

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object does not support item
assignment

I suspect you want to index the results of calling locals(), rather than
the builtin function itself. However:

>>> def f():

.... locals()['x'] = 5
.... return x
....
>>> f()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
NameError: global name 'x' is not defined

No "shadowing", as you see: the compiler knows that x is NOT local,
because it's not assigned to (the indexing of locals() does not count:
the compiler's not expected to detect that), so it's going to look it up
as a global variable (and not find it in this case).

I think that ideally there should be a runtime error when assigning an
item of locals() with a key that's not a local variable name (possibly
excepting functions containing exec, which are kind of screwy anyway).


Alex
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      04-08-2007
(Alex Martelli) writes:
> >>> locals['x']=5

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: 'builtin_function_or_method' object does not support item
> assignment



Whoops, yeah, meant "locals()['x'] = 5".

> I think that ideally there should be a runtime error when assigning an
> item of locals() with a key that's not a local variable name (possibly
> excepting functions containing exec, which are kind of screwy anyway).


I have no opinion of this, locals() has always seemed like a crazy
part of the language to me and I never use it. I'd be happy to see it
gone since it makes compiling a lot easier.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Problem with enterprise application block - data block Showjumper ASP .Net 1 03-19-2005 03:48 PM
Block DIV within a block DIV? Noozer HTML 3 01-06-2005 10:24 PM
XML schema validation of one xml block based on values from another xml block Andy XML 0 11-18-2004 11:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57