Re: [Tutor] break and exit

2014-11-21 Thread Alan Gauld

On 21/11/14 04:10, James Rieve wrote:

I accidently used 'exit' in a loop where I meant to use 'break' and, in that
case, the program seemed to work as expected but in some cases 'exit' seems
to behave differently from 'break'. For example, in this code snippet using
'exit' or 'break' produces the same result:

for i in range(10):
 if i > 3:
 exit
 else:
 print(i)
print('out of loop')



Only superficially.
When you use exit Python doesn't do anything. But it repeats that non 
operation for every number in your range(). When you use break it is 
only executed once and Python leaves the for loop immediately.
So although what is printed is the same the results in terms of the work 
Pyhon does is very different.


You can demonstrate that by adding another print message

for i in range(10):
 if i > 3:
  exit
  print("I didn't expect this!")
 else:
  print(i)
print('out of loop')

Now swap exit and break.



for i in range(10):
 if i > 3:
 break   # try using exit here.
 else:
 print(i)
else:
 print('for loop else statement')

print('out of loop')


Because the for/else is only executed if the loop completes
normally - which it does with exit - so a break will bypass
that for/else clause.


Does anyone have any pointers to descriptions of 'exit', what it is,


Its the name of a function. So using it in isolation like that has the 
same effect as just putting any variable name on its own does:


for n in range(5):
n
else: print('loop completed')

The use of n here is like your use of exit. Its just a name.

But if you called exit using () the effect would be quite different
to break. It would then exit your whole program.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my phopto-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] break and exit

2014-11-21 Thread Peter Otten
James Rieve wrote:

> I accidently used 'exit' in a loop where I meant to use 'break' and, in
> that case, the program seemed to work as expected but in some cases 'exit'
> seems to behave differently from 'break'. For example, in this code
> snippet using 'exit' or 'break' produces the same result:
> 
> for i in range(10):
> if i > 3:
> exit
> else:
> print(i)
> print('out of loop')
> 
> But in this case they behave differently:
> 
> for i in range(10):
> if i > 3:
> break   # try using exit here.
> else:
> print(i)
> else:
> print('for loop else statement')
> 
> print('out of loop')
> 
> Does anyone have any pointers to descriptions of 'exit', what it is, what
> it means, how It's used, etc.?

exit is not part of Python's syntax, it is (almost, see below) a normal 
function. Writing

exit

has no effect, instead of

> for i in range(10):
> if i > 3:
> exit
> else:
> print(i)
> print('out of loop')

you could have written

for i in range(10):
if i > 3:
pass
else:
print(i)
print('out of loop')

but if you invoke exit like a function

> for i in range(10):
> if i > 3:
  exit()
> else:
> print(i)
> print('out of loop')

the script will be terminated -- you can detect that by the fact that

out of loop

is not printed. Personally, I hardly ever use exit (or sys.exit() or `raise 
SystemExit`). If you want a script to stop in the middle of execution I 
recommend that instead of

print("some code")
if some_condition:
   exit()
print("more code")

you write

print("some code")
if not some_condition:
print("more code")

or (even better for all but tiny scripts) use a main function like so:

def main():
print("some code")
if some_condition:
return
print("more code")

if __name__ == "__main__":
main()

Now -- what actually is exit? Let's fire up the interactive interpreter:

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> help(exit)
Help on Quitter in module _sitebuiltins object:

class Quitter(builtins.object)
 |  Methods defined here:
 |  
 |  __call__(self, code=None)
 |  
 |  __init__(self, name, eof)
 |  
 |  __repr__(self)
 |  
 |  --
 |  Data descriptors defined here:
 |  
 |  __dict__
 |  dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |  list of weak references to the object (if defined)

So exit is an instance of Quitter. The Quitter class has a __call__ method 
which is executed when you invoke an instance like a function. Let's have a 
look a the code:

>>> import inspect
>>> print(inspect.getsource(exit.__call__))
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)

So exit() tries to close sys.stdin and then raises a SystemExit exception. 
Unless you catch that exception the program ends.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] break and exit

2014-11-20 Thread James Rieve
I accidently used 'exit' in a loop where I meant to use 'break' and, in that
case, the program seemed to work as expected but in some cases 'exit' seems
to behave differently from 'break'. For example, in this code snippet using
'exit' or 'break' produces the same result:

for i in range(10):
if i > 3:
exit
else:
print(i)
print('out of loop')

But in this case they behave differently:

for i in range(10):
if i > 3:
break   # try using exit here.
else:
print(i)
else:
print('for loop else statement')

print('out of loop')

Does anyone have any pointers to descriptions of 'exit', what it is, what it
means, how It's used, etc.?


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor