473,796 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

block scope?

One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?

Apr 6 '07 #1
24 2671
Neal Becker wrote:
One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?
Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:
pyi
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.Nam eError'>: name 'i' is not defined

py[i for i in xrange(4)]
[0, 1, 2, 3]
pyi # hoping for NameError
3
Apr 7 '07 #2
James Stroud <js*****@mbi.uc la.eduwrites:
Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:
Block scope is a win because it gets rid of the uncertainty of whether
the variable is used outside the block or not. The "good code" theory
(just manually don't use the variable elsewhere) doesn't always hold
up under release deadline pressure and so on and doesn't make sense
anyway. What's the point of NOT having block scope if you don't want
to allow for creating variables in inner blocks and using them in
other blocks? I think it's best to require creating the variable
in a mutually enclosing scope if you want to use it that way.
Apr 7 '07 #3
Paul Rubin wrote:
James Stroud <js*****@mbi.uc la.eduwrites:
>>Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:


Block scope is a win because it gets rid of the uncertainty of whether
the variable is used outside the block or not.
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.

Javascript got it wrong. They have declarations, but the default,
in the absence of a declaration, is global, not local or an error.
Bad design. It's a result of retrofitting declarations to a language,
which usually has painful aftereffects.

John Nagle
Apr 7 '07 #4
John Nagle <na***@animats. comwrites:
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++.
Apr 7 '07 #5
Paul Rubin wrote:
John Nagle <na***@animats. comwrites:
> 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).

James
Apr 7 '07 #6
James Stroud wrote:
Paul Rubin wrote:
>John Nagle <na***@animats. comwrites:
>> 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).

James
Yes, the above is pretty much what I had in mind. +1.
Apr 7 '07 #7
On Apr 7, 6:48 am, James Stroud <jstr...@mbi.uc la.eduwrote:
Neal Becker wrote:
One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?

Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:
Generator expressions have a new scope, and in Python 3.0 list
comprehensions will have one as well (according to http://www.python.org/dev/peps/pep-0289/
). It's a fix that might break existing code so it couldn't be
introduced in "minor" versions like 2.4 and 2.5.

Apr 7 '07 #8
Neal Becker <nd*******@gmai l.comwrote:
...
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).

James

Yes, the above is pretty much what I had in mind. +1.
I prefer Java's approach (14.4.2 in the JLS 2nd edition): forbid "inner"
blocks from shadowing variables in "outer" ones. I quote:
"""
If a declaration of an identifier as a local variable of the same
method, constructor, or initializer block appears within the scope of a
parameter or local variable of the same name, a compile-time error
occurs.
Thus the following example does not compile:

class Test {
public static void main(String[] args) {
int i;
for (int i = 0; i < 10; i++)
System.out.prin tln(i);
}
}
This restriction helps to detect some otherwise very obscure bugs.
"""
I entirely agree with the JLS here, having fought just such bugs in C++
and other languages that lack the restriction in question. I just wish
Python had adopted the same restriction regarding nested functions, when
proper lexical scoping was introduced -- I argued for it at the time,
but backwards compatibility blocked its introduction. There are
definitely NOT many Java-specific language characteristics that I like,
but this one is a winner!-) [[but, I disagree with the lack in Java of
a similar restriction against shadowing between instance variables and
local variables, and the weak rationale for that in the JLS:-)]].
Alex
Apr 7 '07 #9
Alex Martelli wrote:
Neal Becker <nd*******@gmai l.comwrote:
...
>>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).

James
Yes, the above is pretty much what I had in mind. +1.

I prefer Java's approach (14.4.2 in the JLS 2nd edition): forbid "inner"
blocks from shadowing variables in "outer" ones. I quote:
"""
If a declaration of an identifier as a local variable of the same
method, constructor, or initializer block appears within the scope of a
parameter or local variable of the same name, a compile-time error
occurs.
Thus the following example does not compile:

class Test {
public static void main(String[] args) {
int i;
for (int i = 0; i < 10; i++)
System.out.prin tln(i);
}
}
This restriction helps to detect some otherwise very obscure bugs.
"""
I entirely agree with the JLS here, having fought just such bugs in C++
and other languages that lack the restriction in question. I just wish
Python had adopted the same restriction regarding nested functions, when
proper lexical scoping was introduced -- I argued for it at the time,
but backwards compatibility blocked its introduction. There are
definitely NOT many Java-specific language characteristics that I like,
but this one is a winner!-) [[but, I disagree with the lack in Java of
a similar restriction against shadowing between instance variables and
local variables, and the weak rationale for that in the JLS:-)]].
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.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
34263
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
18
3463
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
12
2730
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external linkage? Not much on this in the FAQ or tutorials.
7
7718
by: seamoon | last post by:
Hi, I'm doing a simple compiler with C as a target language. My language uses the possibility to declare variables anywhere in a block with scope to the end of the block. As I remembered it this would be easily translated to C, but it seems variable declaration is only possible in the beginning of a block in C. Any suggestions how to get around this?
5
2710
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope applies to objects created in the using scope. For example: <code> static DataTable getTable()
2
3606
by: Anoj | last post by:
Hi All, As you all know in vb.net we can declare block level variables. Like : Dim I As Integer For I = 1 To 3 Dim N As Long ' N has block scope in VB.NET N = N + I Next
11
1331
by: | last post by:
Is it possible to define a variable in a block in order to make it invisible outside that block? For example, in C I can write { int a .... } then a will only be available inside the curley brackets
165
6916
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But when the struct is declared in main (block scope) it will segfault when passing namelist to freeFileNames().
4
2370
by: shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.