473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1195
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Luca | last post by:
About one month ago I have inserted in my web site a clickcounter who controls the access to almost every link, but this script who changed the path in every link so the search engines couldn't...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
3
by: pantagruel | last post by:
The following does not work in firefox: <script defer="defer"> var x=document.getElementsByName("repositoryNamespace") alert(x.length + " elements!")
7
by: selen | last post by:
I am using above script for Focus to txtAdi which is textbox in c# web application.But it doesnt work. Have you any idea why it doesnt work thanks....... char c=(char)34;
1
by: Russ | last post by:
I've been trying to get my head around this for 3 days now and it seems like everything I try does not work for one reason or another. I built a test page using the TabStrip and MultiPage controls....
12
by: tshad | last post by:
I am not sure why I am getting this error: I have the following code I want to run from another include file that holds all my functions. functions.inc...
0
by: lechatthierry | last post by:
Is it possible to block a mouse event on an Hyperlink with a general script event? This is quite troublesome for me. I am trying to find a way to block the windows shortcut SHIFT + MOUSE LEFT...
1
by: Neko | last post by:
Is it possible to block a mouse event on an Hyperlink with a general script event? This is quite troublesome for me. I am trying to find a way to block the windows shortcut SHIFT + MOUSE LEFT...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.