Re: [Tutor] scope/namespaces

2007-04-24 Thread Alan Gauld
"Ben Sherman" <[EMAIL PROTECTED]> wrote

> Am I wrong in my memory?  When I was a wee lad prior to 99 for 
> sure),
> I thought I would initialize my loops with:
>
> for (int x=0; x <10; x++) {

You certainly could in C++ but I'm less sure about C.
You certainly couldn't do that in C prior to ANSI C
(in 1991/2?). However I don't think the C++ bits got
incorporated into C until the C update in '99. However
that didn't stop some compilers supporting them.
For example the C++ // comment style was supported
by most ANSI compilers even though it wasn't in the
original ANSI standard.

> I am rapidly veering off topic.

Me too :-)

Alan G.
Who hasn't used vanilla C in anger for at least 10 years!
(And C++ for 4 or 5...)




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Roel Schroeven
Ben Sherman schreef:
> Am I wrong in my memory?  When I was a wee lad prior to 99 for sure),
> I thought I would initialize my loops with:
> 
> for (int x=0; x <10; x++) {
> }

If that was in C, it must have been a special feature of your compiler.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Ben Sherman
Am I wrong in my memory?  When I was a wee lad prior to 99 for sure),
I thought I would initialize my loops with:

for (int x=0; x <10; x++) {
}

I am rapidly veering off topic.


On 4/24/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> Correcting my own post!
>
> "Alan Gauld" <[EMAIL PROTECTED]> wrote
>
> > That's a very recent change to C/C++ (1999 apparently),
>
> Actually only a recent change in C. Its always been true of C++.
> But in C up until recently(*) you couldn't define a loop
> variable in the loop it had to be outside:
>
> int x;
> for (x=0;)
>
> (*)I'm not sure whether the C++ style loop definition was
> introduced in the original ANSI standard or the later
> revision (none of my books malke it clear), but I think it was
> the revision.
>
> But C++ always had loop variables as part of block scope.
>
> Sory for any confusion,
>
> Alan G.
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Alan Gauld
Correcting my own post!

"Alan Gauld" <[EMAIL PROTECTED]> wrote 

> That's a very recent change to C/C++ (1999 apparently), 

Actually only a recent change in C. Its always been true of C++.
But in C up until recently(*) you couldn't define a loop
variable in the loop it had to be outside:

int x;
for (x=0;)

(*)I'm not sure whether the C++ style loop definition was 
introduced in the original ANSI standard or the later 
revision (none of my books malke it clear), but I think it was 
the revision.

But C++ always had loop variables as part of block scope.

Sory for any confusion,

Alan G.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Alan Gauld
> Ezra Taylor wrote:
>> How can we limit this functionality so that 
>> python
>> behaves similar to other know languages.

There are many other languages that work like Python.
Including the original versions of C and C++...

And other languages that don't have explicit loop constructs
at all! Others only have while loops...

> Maybe I should be asking what
> are the benifits of allow variables not being bound to a block of 
> code.

For one thing you can find out how far a loop got to if it was
exited via a break.

for n in range(25):
if doit(item[n]) == False: break

print 'The loop stopped at item', n

There are lots of things that languages do differently, otherwise
there would be no point in having different languages! Embrace
the differences as an opportunity to see things differently. ;-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Alan Gauld
"ammar azif" <[EMAIL PROTECTED]> wrote 

> Something in python disturbs me ,
> 
> when i write a for loop, i am able to access the variable 
> declared in that loop after the loop finishes which i am 
> not able to do in languages like c/c++ or java. 

That's a very recent change to C/C++ (1999 apparently), 
you used to be able to do that. In fact I hadn't realised that 
you couldn't any more! It was only when I wrote a test 
program I discovered you were right...

> Is it different in python?

Yes. Once you create a name within a scope it stays 
there and loops or code blocks are not a separate scope 
in Python 

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Kent Johnson
Ezra Taylor wrote:
> Hello Kent:
> How can we limit this functionality so that python 
> behaves similar to other know languages.  Maybe I should be asking what 
> are the benifits of allow variables not being bound to a block of code.

Why is this a problem? Don't try to turn Python into Java, you might as
well stay with Java.

One advantage is, you can define a variable in a conditional block or a
try block without having to declare it first. For example:
if something:
   x = 3
else:
   x = 5

or
try:
   x = foo()
finally:
   cleanup()

# do something with x

It has long annoyed my that in Java these snippets would have to be
prefixed with
int x;

Kent
> 
> Ezra
> 
> On 4/24/07, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> ammar azif wrote:
>  > Something in python disturbs me ,
>  >
>  > when i write a for loop,
>  >
>  > i am able to access the variable declared in that loop after the loop
>  > finishes which i am not able to do in languages like c/c++ or
> java. Is
>  > it different in python?
> 
> Yes, it is different. In Python a block is not a scope. Names bound
> within the block, including the loop variable, are accessible outside
> the block.
> 
> Kent
> ___
> Tutor maillist  -  Tutor@python.org 
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 
> -- 
> Ezra Taylor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Ezra Taylor

Sorry about that kent.  I just realized I emailed you directly.

Ezra

On 4/24/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


ammar azif wrote:
> Something in python disturbs me ,
>
> when i write a for loop,
>
> i am able to access the variable declared in that loop after the loop
> finishes which i am not able to do in languages like c/c++ or java. Is
> it different in python?

Yes, it is different. In Python a block is not a scope. Names bound
within the block, including the loop variable, are accessible outside
the block.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor





--
Ezra Taylor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Kent Johnson
ammar azif wrote:
> Something in python disturbs me ,
> 
> when i write a for loop,
> 
> i am able to access the variable declared in that loop after the loop 
> finishes which i am not able to do in languages like c/c++ or java. Is 
> it different in python?

Yes, it is different. In Python a block is not a scope. Names bound 
within the block, including the loop variable, are accessible outside 
the block.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Rikard Bosnjakovic
On 4/24/07, ammar azif <[EMAIL PROTECTED]> wrote:

> i am able to access the variable declared in that loop after the loop
> finishes which i am not able to do in languages like c/c++ or java. Is it
> different in python?

I'm not sure what you mean with "different", but the loop-variable is
not destroyed upon the exit of the for-loop:

>>> z
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'z' is not defined
>>> for z in (1,2,3): pass
...
>>> print z
3


-- 
- Rikard - http://bos.hack.org/cv/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] scope/namespaces

2007-04-24 Thread ammar azif
Something in python disturbs me ,

when i write a for loop,

i am able to access the variable declared in that loop after the loop finishes 
which i am not able to do in languages like c/c++ or java. Is it different in 
python?

   
-
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor