472,127 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Why this script can work?

Please help with this script:

class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self,length,atleast):
Exception.__init__(self)
self.length=length
self.atleast=atleast

try:
s=raw_input('Enter something --')
if len(s)<3:
raise ShortInputException(len(s),3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException,x:
print 'ShortInputException: The input was of length %d, was
expecting at least %d' %(x.length,x.atleast)
else:
print 'No exception was raised.'
My questions are:

1) ShortInputException,x: what's the 'x'? where is it coming?

2) The 'if' and 'else' are not in the same indent scope,why this can work?

Thanks in advance.
Jan 19 '07 #1
4 1122
Jm lists wrote:
Please help with this script:

class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self,length,atleast):
Exception.__init__(self)
self.length=length
self.atleast=atleast

try:
s=raw_input('Enter something --')
if len(s)<3:
raise ShortInputException(len(s),3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException,x:
print 'ShortInputException: The input was of length %d, was
expecting at least %d' %(x.length,x.atleast)
else:
print 'No exception was raised.'
My questions are:

1) ShortInputException,x: what's the 'x'? where is it coming?
except <ExceptionSpec>, <variable>:

will catch an exception of the kind specified in <ExceptionSpec(it might
actually be more than one), and store the exception object in the variable
named <variable>
2) The 'if' and 'else' are not in the same indent scope,why this can work?

Because additionally to if, also for and try have else-clauses. The latter
two are only being called if the body of the control structure hasn't been
left due to "unnatural" circumstances. See this:


for i in xrange(10):
pass
else:
print "test 1"

for i in xrange(10):
break
else:
print "test 2"

try:
pass
except:
pass
else:
print "test 3"

try:
raise "I know I shouldn't rais strings..."
except:
pass
else:
print "test 4"

It will only print

test 1
test 3
Diez
Jan 19 '07 #2
Thanks for all the helps.
I'm not habitual for this usage of 'else',other languages seem don't
support this syntax.
i.g,writting the codes below by Perl would get an error:

# perl -le 'for $i (1..10){print $i} else{print "finished"}'
syntax error at -e line 1, near "}else"
Execution of -e aborted due to compilation errors.

2007/1/19, Diez B. Roggisch <de***@nospam.web.de>:
Jm lists wrote:
Please help with this script:

class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self,length,atleast):
Exception.__init__(self)
self.length=length
self.atleast=atleast

try:
s=raw_input('Enter something --')
if len(s)<3:
raise ShortInputException(len(s),3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException,x:
print 'ShortInputException: The input was of length %d, was
expecting at least %d' %(x.length,x.atleast)
else:
print 'No exception was raised.'
My questions are:

1) ShortInputException,x: what's the 'x'? where is it coming?

except <ExceptionSpec>, <variable>:

will catch an exception of the kind specified in <ExceptionSpec(it might
actually be more than one), and store the exception object in the variable
named <variable>
2) The 'if' and 'else' are not in the same indent scope,why this can work?


Because additionally to if, also for and try have else-clauses. The latter
two are only being called if the body of the control structure hasn't been
left due to "unnatural" circumstances. See this:


for i in xrange(10):
pass
else:
print "test 1"

for i in xrange(10):
break
else:
print "test 2"

try:
pass
except:
pass
else:
print "test 3"

try:
raise "I know I shouldn't rais strings..."
except:
pass
else:
print "test 4"

It will only print

test 1
test 3
Diez
--
http://mail.python.org/mailman/listinfo/python-list
Jan 19 '07 #3
Jm lists wrote:
Thanks for all the helps.
I'm not habitual for this usage of 'else',other languages seem don't
support this syntax.
i.g,writting the codes below by Perl would get an error:
I personally consider this part of python also somewhat obscure. But I just
don't use it and don't bother.

Diez
Jan 19 '07 #4
"Jm lists" <pr***********@gmail.comescribió en el mensaje
news:fb******************************************@ mail.gmail.com...
I'm not habitual for this usage of 'else',other languages seem don't
support this syntax.
i.g,writting the codes below by Perl would get an error:

[[[censored example]]]
If all languages had the same features, what would be the point of having
different languages at all?

--
Gabriel Genellina
Jan 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Luca | last post: by
7 posts views Thread by selen | last post: by
1 post views Thread by Russ | last post: by
12 posts views Thread by tshad | last post: by
reply views Thread by leo001 | last post: by

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.